Example usage for com.fasterxml.jackson.databind DeserializationContext getNodeFactory

List of usage examples for com.fasterxml.jackson.databind DeserializationContext getNodeFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind DeserializationContext getNodeFactory.

Prototype

public final JsonNodeFactory getNodeFactory() 

Source Link

Usage

From source file:com.github.jonpeterson.jackson.module.interceptor.JsonInterceptingDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode jsonNode = parser.readValueAsTree();

    for (JsonInterceptor interceptor : interceptors)
        jsonNode = interceptor.intercept(jsonNode, context.getNodeFactory());

    JsonParser postInterceptionParser = new TreeTraversingParser(jsonNode, parser.getCodec());
    postInterceptionParser.nextToken();/*from   w  ww.  j a  v  a  2s. c om*/
    return delegate.deserialize(postInterceptionParser, context);
}

From source file:com.addthis.codec.jackson.CaseIgnoringEnumDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken curr = jp.getCurrentToken();

    if ((curr == JsonToken.VALUE_STRING) || (curr == JsonToken.FIELD_NAME) || (curr == JsonToken.VALUE_FALSE)
            || (curr == JsonToken.VALUE_TRUE)) {
        String name = jp.getText();
        if (_lookupByName.find(name) != null) {
            return super.deserialize(jp, ctxt);
        }//  w  w  w  .ja v  a 2  s.co  m
        TextNode upperName = ctxt.getNodeFactory().textNode(name.toUpperCase());

        JsonParser treeParser = jp.getCodec().treeAsTokens(upperName);
        treeParser.nextToken();
        return super.deserialize(treeParser, ctxt);
    } else {
        return super.deserialize(jp, ctxt);
    }
}

From source file:com.addthis.codec.jackson.CodecBeanDeserializer.java

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonLocation currentLocation = jp.getTokenLocation();
    JsonToken t = jp.getCurrentToken();//from ww w.  j  av  a  2  s  .c  o  m
    try {
        if (t == JsonToken.START_OBJECT) {
            ObjectNode objectNode = jp.readValueAsTree();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        } else if (t == JsonToken.END_OBJECT) {
            // for some reason this is how they chose to handle single field objects
            jp.nextToken();
            ObjectNode objectNode = ctxt.getNodeFactory().objectNode();
            handleDefaultsAndRequiredAndNull(ctxt, objectNode);
            jp = jp.getCodec().treeAsTokens(objectNode);
            jp.nextToken();
        }
        Object value = getDelegatee().deserialize(jp, ctxt);
        if (value instanceof SuperCodable) {
            ((SuperCodable) value).postDecode();
        }
        return value;
    } catch (JsonMappingException ex) {
        throw Jackson.maybeImproveLocation(currentLocation, ex);
    }
}

From source file:com.addthis.bundle.value.ValueDeserializer.java

@Override
public ValueObject deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    JsonToken t = jp.getCurrentToken();//from   ww w  .  j a v a2 s  . c o  m
    switch (t) {
    case VALUE_TRUE:
    case VALUE_FALSE:
    case VALUE_STRING:
        return jp.readValueAs(ValueString.class);
    case VALUE_NUMBER_INT:
        return jp.readValueAs(ValueLong.class);
    case VALUE_NUMBER_FLOAT:
        return jp.readValueAs(ValueDouble.class);
    case START_ARRAY:
        return jp.readValueAs(ValueArray.class);
    case START_OBJECT:
    case FIELD_NAME:
        return jp.readValueAs(ValueMap.class);
    case END_OBJECT:
        // calling jp.readValueAs here will return null rather than an empty map, so make empty map in tokens
        jp.nextToken();
        ObjectNode objectNode = ctxt.getNodeFactory().objectNode();
        JsonParser emptyObjectParser = jp.getCodec().treeAsTokens(objectNode);
        emptyObjectParser.nextToken();
        return emptyObjectParser.readValueAs(ValueMap.class);
    case VALUE_EMBEDDED_OBJECT:
    default:
        throw ctxt.mappingException(handledType());
    }
}

From source file:com.addthis.codec.jackson.CodecTypeDeserializer.java

@Override
public Object deserializeTypedFromAny(JsonParser jp, DeserializationContext ctxt) throws IOException {
    // a jackson thing we might as well include
    if (jp.canReadTypeId()) {
        Object typeId = jp.getTypeId();
        if (typeId != null) {
            return _deserializeWithNativeTypeId(jp, ctxt, typeId);
        }/*from   ww  w.ja v a2s  .  c o m*/
    }
    // can use this to approximate error location if a sub-method throws an exception
    JsonLocation currentLocation = jp.getTokenLocation();
    JsonNode jsonNode;
    // empty objects can appear with END_OBJECT. that has special handling lots of places, but not in readTree
    if (jp.getCurrentToken() == JsonToken.END_OBJECT) {
        jsonNode = ctxt.getNodeFactory().objectNode();
    } else {
        jsonNode = jp.readValueAsTree();
    }
    ObjectCodec objectCodec = jp.getCodec();

    try {
        Object bean = null;
        // _array handler
        if (jsonNode.isArray()) {
            bean = _deserializeTypedFromArray((ArrayNode) jsonNode, objectCodec, ctxt);
            // object handler
        } else if (jsonNode.isObject()) {
            bean = _deserializeTypedFromObject((ObjectNode) jsonNode, objectCodec, ctxt);
        }
        if (bean != null) {
            return bean;
        } else {
            // Jackson 2.6+ throws NPE on null typeId parameter (underlying Map changed from HashMap
            // to ConcurrentHashMap), so use empty string instead of null
            JsonDeserializer<Object> deser = _findDeserializer(ctxt, "");
            JsonParser treeParser = jp.getCodec().treeAsTokens(jsonNode);
            treeParser.nextToken();
            return deser.deserialize(treeParser, ctxt);
        }
    } catch (JsonMappingException ex) {
        throw Jackson.maybeImproveLocation(currentLocation, ex);
    }
}

From source file:com.github.jonpeterson.jackson.module.versioning.VersionedModelDeserializer.java

@Override
public T deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    JsonNode jsonNode = parser.readValueAsTree();

    if (!(jsonNode instanceof ObjectNode))
        throw context.mappingException("value must be a JSON object");

    ObjectNode modelData = (ObjectNode) jsonNode;

    JsonNode modelVersionNode = modelData.remove(jsonVersionedModel.propertyName());

    String modelVersion = null;/*w  w w  .  j a  v a  2s . c o  m*/
    if (modelVersionNode != null)
        modelVersion = modelVersionNode.asText();

    if (modelVersion == null)
        modelVersion = jsonVersionedModel.defaultDeserializeToVersion();

    if (modelVersion.isEmpty())
        throw context.mappingException("'" + jsonVersionedModel.propertyName()
                + "' property was null and defaultDeserializeToVersion was not set");

    // convert the model if converter specified and model needs converting
    if (converter != null && (jsonVersionedModel.alwaysConvert()
            || !modelVersion.equals(jsonVersionedModel.currentVersion())))
        modelData = converter.convert(modelData, modelVersion, jsonVersionedModel.currentVersion(),
                context.getNodeFactory());

    // set the serializeToVersionProperty value to the source model version if the defaultToSource property is true
    if (serializeToVersionAnnotation != null && serializeToVersionAnnotation.defaultToSource())
        modelData.put(serializeToVersionProperty.getName(), modelVersion);

    JsonParser postInterceptionParser = new TreeTraversingParser(modelData, parser.getCodec());
    postInterceptionParser.nextToken();
    return delegate.deserialize(postInterceptionParser, context);
}