Java Code Examples for com.fasterxml.jackson.databind.jsontype.TypeDeserializer#deserializeTypedFromAny()

The following examples show how to use com.fasterxml.jackson.databind.jsontype.TypeDeserializer#deserializeTypedFromAny() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlatteningDeserializer.java    From botbuilder-java with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext cxt, TypeDeserializer tDeserializer) throws IOException {
    // This method will be called by Jackson for each "Json object with TypeId" in the input wire stream
    // it is trying to deserialize.
    // The below variable 'currentJsonNode' will hold the JsonNode corresponds to current
    // Json object this method is called to handle.
    //
    JsonNode currentJsonNode = mapper.readTree(jp);
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        if (c.isAssignableFrom(Object.class)) {
            continue;
        } else {
            final JsonTypeInfo typeInfo = c.getAnnotation(JsonTypeInfo.class);
            if (typeInfo != null) {
                String typeId = typeInfo.property();
                if (containsDot(typeId)) {
                    final String typeIdOnWire = unescapeEscapedDots(typeId);
                    JsonNode typeIdValue = ((ObjectNode) currentJsonNode).remove(typeIdOnWire);
                    if (typeIdValue != null) {
                        ((ObjectNode) currentJsonNode).put(typeId, typeIdValue);
                    }
                }
            }
        }
    }
    return tDeserializer.deserializeTypedFromAny(newJsonParserForNode(currentJsonNode), cxt);
}
 
Example 2
Source File: NullifyingDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
        TypeDeserializer typeDeserializer) throws IOException
{
    // Not sure if we need to bother but:

    switch (p.getCurrentTokenId()) {
    case JsonTokenId.ID_START_ARRAY:
    case JsonTokenId.ID_START_OBJECT:
    case JsonTokenId.ID_FIELD_NAME:
        return typeDeserializer.deserializeTypedFromAny(p, ctxt);
    default:
        return null;
    }
}
 
Example 3
Source File: UntypedObjectDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException
{
    switch (p.getCurrentTokenId()) {
    case JsonTokenId.ID_START_ARRAY:
    case JsonTokenId.ID_START_OBJECT:
    case JsonTokenId.ID_FIELD_NAME:
        return typeDeserializer.deserializeTypedFromAny(p, ctxt);

    case JsonTokenId.ID_STRING:
        return p.getText();

    case JsonTokenId.ID_NUMBER_INT:
        if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS)) {
            return p.getBigIntegerValue();
        }
        return p.getNumberValue();

    case JsonTokenId.ID_NUMBER_FLOAT:
        if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
            return p.getDecimalValue();
        }
        return p.getNumberValue();

    case JsonTokenId.ID_TRUE:
        return Boolean.TRUE;
    case JsonTokenId.ID_FALSE:
        return Boolean.FALSE;
    case JsonTokenId.ID_EMBEDDED_OBJECT:
        return p.getEmbeddedObject();

    case JsonTokenId.ID_NULL: // should not get this far really but...
        return null;
    default:
    }
    return ctxt.handleUnexpectedToken(Object.class, p);
}
 
Example 4
Source File: FactoryBasedEnumDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
    if (_deser == null) { // String never has type info
        return deserialize(p, ctxt);
    }
    return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}
 
Example 5
Source File: JsonNodeDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
        TypeDeserializer typeDeserializer)
    throws IOException
{
    // Output can be as JSON Object, Array or scalar: no way to know a priori:
    return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}
 
Example 6
Source File: FlatteningDeserializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext cxt, TypeDeserializer tDeserializer) throws IOException {
    // This method will be called by Jackson for each "Json object with TypeId" in the input wire stream
    // it is trying to deserialize.
    // The below variable 'currentJsonNode' will hold the JsonNode corresponds to current
    // Json object this method is called to handle.
    //
    JsonNode currentJsonNode = mapper.readTree(jp);
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        if (c.isAssignableFrom(Object.class)) {
            continue;
        } else {
            final JsonTypeInfo typeInfo = c.getAnnotation(com.fasterxml.jackson.annotation.JsonTypeInfo.class);
            if (typeInfo != null) {
                String typeId = typeInfo.property();
                if (containsDot(typeId)) {
                    final String typeIdOnWire = unescapeEscapedDots(typeId);
                    JsonNode typeIdValue = ((ObjectNode) currentJsonNode).remove(typeIdOnWire);
                    if (typeIdValue != null) {
                        ((ObjectNode) currentJsonNode).put(typeId, typeIdValue);
                    }
                }
            }
        }
    }
    return tDeserializer.deserializeTypedFromAny(newJsonParserForNode(currentJsonNode), cxt);
}
 
Example 7
Source File: JSR310StringParsableDeserializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext context,
        TypeDeserializer deserializer)
    throws IOException
{
    // This is a nasty kludge right here, working around issues like
    // [datatype-jsr310#24]. But should work better than not having the work-around.
    JsonToken t = p.currentToken();
    if ((t != null) && t.isScalarValue()) {
        return deserialize(p, context);
    }
    return deserializer.deserializeTypedFromAny(p, context);
}
 
Example 8
Source File: JSR310DeserializerBase.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser parser, DeserializationContext context,
        TypeDeserializer typeDeserializer)
    throws IOException
{
    return typeDeserializer.deserializeTypedFromAny(parser, context);
}
 
Example 9
Source File: JSR310StringParsableDeserializer.java    From bootique with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser parser, DeserializationContext context, TypeDeserializer deserializer)
        throws IOException {
    /**
     * This is a nasty kludge right here, working around issues like
     * [datatype-jsr310#24]. But should work better than not having the work-around.
     */
    if (parser.getCurrentToken().isScalarValue()) {
        return deserialize(parser, context);
    }
    return deserializer.deserializeTypedFromAny(parser, context);
}
 
Example 10
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Base implementation that does not assume specific type
 * inclusion mechanism. Sub-classes are expected to override
 * this method if they are to handle type information.
 */
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
        TypeDeserializer typeDeserializer) throws IOException {
    return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}
 
Example 11
Source File: UntypedObjectDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
        TypeDeserializer typeDeserializer) throws IOException
{
    switch (p.getCurrentTokenId()) {
    // First: does it look like we had type id wrapping of some kind?
    case JsonTokenId.ID_START_ARRAY:
    case JsonTokenId.ID_START_OBJECT:
    case JsonTokenId.ID_FIELD_NAME:
        // Output can be as JSON Object, Array or scalar: no way to know at this point:
        return typeDeserializer.deserializeTypedFromAny(p, ctxt);

    case JsonTokenId.ID_EMBEDDED_OBJECT:
        return p.getEmbeddedObject();
        
    /* Otherwise we probably got a "native" type (ones that map
     * naturally and thus do not need or use type ids)
     */
    case JsonTokenId.ID_STRING:
        if (_stringDeserializer != null) {
            return _stringDeserializer.deserialize(p, ctxt);
        }
        return p.getText();

    case JsonTokenId.ID_NUMBER_INT:
        if (_numberDeserializer != null) {
            return _numberDeserializer.deserialize(p, ctxt);
        }
        // May need coercion to "bigger" types:
        if (ctxt.hasSomeOfFeatures(F_MASK_INT_COERCIONS)) {
            return _coerceIntegral(p, ctxt);
        }
        return p.getNumberValue(); // should be optimal, whatever it is

    case JsonTokenId.ID_NUMBER_FLOAT:
        if (_numberDeserializer != null) {
            return _numberDeserializer.deserialize(p, ctxt);
        }
        if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
            return p.getDecimalValue();
        }
        return p.getNumberValue();

    case JsonTokenId.ID_TRUE:
        return Boolean.TRUE;
    case JsonTokenId.ID_FALSE:
        return Boolean.FALSE;

    case JsonTokenId.ID_NULL: // should not get this far really but...
        return null;
    default:
    }
    return ctxt.handleUnexpectedToken(Object.class, p);
}
 
Example 12
Source File: JSR310DeserializerBase.java    From bootique with Apache License 2.0 4 votes vote down vote up
@Override
public Object deserializeWithType(JsonParser parser, DeserializationContext context, TypeDeserializer deserializer)
        throws IOException {
    return deserializer.deserializeTypedFromAny(parser, context);
}
 
Example 13
Source File: JsonDeserializer.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Deserialization called when type being deserialized is defined to
 * contain additional type identifier, to allow for correctly
 * instantiating correct subtype. This can be due to annotation on
 * type (or its supertype), or due to global settings without
 * annotations.
 *<p>
 * Default implementation may work for some types, but ideally subclasses
 * should not rely on current default implementation.
 * Implementation is mostly provided to avoid compilation errors with older
 * code.
 * 
 * @param typeDeserializer Deserializer to use for handling type information
 */
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
        TypeDeserializer typeDeserializer)
    throws IOException
{
    // We could try calling 
    return typeDeserializer.deserializeTypedFromAny(p, ctxt);
}