Search in sources :

Example 31 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.

the class DeserializationContext method findRootValueDeserializer.

/**
     * Method for finding a deserializer for root-level value.
     */
@SuppressWarnings("unchecked")
public final JsonDeserializer<Object> findRootValueDeserializer(JavaType type) throws JsonMappingException {
    JsonDeserializer<Object> deser = _cache.findValueDeserializer(this, _factory, type);
    if (deser == null) {
        // can this occur?
        return null;
    }
    deser = (JsonDeserializer<Object>) handleSecondaryContextualization(deser, null, type);
    TypeDeserializer typeDeser = _factory.findTypeDeserializer(_config, type);
    if (typeDeser != null) {
        // important: contextualize to indicate this is for root value
        typeDeser = typeDeser.forProperty(null);
        return new TypeWrappedDeserializer(typeDeser, deser);
    }
    return deser;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) TypeWrappedDeserializer(com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer)

Example 32 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project x-pipe by ctripcorp.

the class PairDeserial method deserialize.

@Override
public Pair<Object, Object> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    JsonToken t = p.getCurrentToken();
    if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
        // slightly redundant (since String was passed above), but
        return _deserializeFromEmpty(p, ctxt);
    }
    if (t == JsonToken.START_OBJECT) {
        t = p.nextToken();
    }
    if (t != JsonToken.FIELD_NAME) {
        if (t == JsonToken.END_OBJECT) {
            ctxt.reportMappingException("Can not deserialize a Map.Entry out of empty JSON Object");
            return null;
        }
        return (Pair<Object, Object>) ctxt.handleUnexpectedToken(handledType(), p);
    }
    final KeyDeserializer keyDes = _keyDeserializer;
    final JsonDeserializer<Object> valueDes = _valueDeserializer;
    final TypeDeserializer typeDeser = _valueTypeDeserializer;
    final String keyStr = p.getCurrentName();
    Object key = keyDes.deserializeKey(keyStr, ctxt);
    Object value = null;
    // And then the value...
    t = p.nextToken();
    try {
        // Note: must handle null explicitly here; value deserializers won't
        if (t == JsonToken.VALUE_NULL) {
            value = valueDes.getNullValue(ctxt);
        } else if (typeDeser == null) {
            value = valueDes.deserialize(p, ctxt);
        } else {
            value = valueDes.deserializeWithType(p, ctxt, typeDeser);
        }
    } catch (Exception e) {
        throw new IllegalStateException("pair value deserialize error", e);
    }
    // Close, but also verify that we reached the END_OBJECT
    t = p.nextToken();
    if (t != JsonToken.END_OBJECT) {
        if (t == JsonToken.FIELD_NAME) {
            // most likely
            ctxt.reportMappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '" + p.getCurrentName() + "')");
        } else {
            // how would this occur?
            ctxt.reportMappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: " + t);
        }
        return null;
    }
    return new Pair<>(key, value);
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) IOException(java.io.IOException)

Example 33 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project joynr by bmwcarit.

the class AbstractBounceProxyServerTest method getObjectMapper.

// ///////////////////////
// HELPERS
private ObjectMapper getObjectMapper() {
    objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
    // objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS,
    // true);
    objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true);
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "_typeName");
    TypeResolverBuilder<?> joynrTypeResolverBuilder = objectMapper.getSerializationConfig().getDefaultTyper(SimpleType.construct(Object.class));
    SimpleModule module = new SimpleModule("NonTypedModule", new Version(1, 0, 0, "", "", ""));
    module.addSerializer(new JoynrEnumSerializer());
    module.addSerializer(new JoynrListSerializer());
    TypeDeserializer typeDeserializer = joynrTypeResolverBuilder.buildTypeDeserializer(objectMapper.getDeserializationConfig(), SimpleType.construct(Object.class), null);
    module.addDeserializer(Object.class, new JoynrUntypedObjectDeserializer(typeDeserializer));
    objectMapper.registerModule(module);
    return objectMapper;
}
Also used : JoynrUntypedObjectDeserializer(io.joynr.messaging.serialize.JoynrUntypedObjectDeserializer) Version(com.fasterxml.jackson.core.Version) JoynrListSerializer(io.joynr.messaging.serialize.JoynrListSerializer) JoynrEnumSerializer(io.joynr.messaging.serialize.JoynrEnumSerializer) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 34 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project beam by apache.

the class PipelineOptionsFactory method computeDeserializerForMethod.

private static JsonDeserializer<Object> computeDeserializerForMethod(Method method) {
    try {
        BeanProperty prop = createBeanProperty(method);
        AnnotatedMember annotatedMethod = prop.getMember();
        DefaultDeserializationContext context = DESERIALIZATION_CONTEXT.get();
        Object maybeDeserializerClass = context.getAnnotationIntrospector().findDeserializer(annotatedMethod);
        JsonDeserializer<Object> jsonDeserializer = context.deserializerInstance(annotatedMethod, maybeDeserializerClass);
        if (jsonDeserializer == null) {
            jsonDeserializer = context.findContextualValueDeserializer(prop.getType(), prop);
        }
        TypeDeserializer typeDeserializer = context.getFactory().findTypeDeserializer(context.getConfig(), prop.getType());
        if (typeDeserializer != null) {
            jsonDeserializer = new TypeWrappedDeserializer(typeDeserializer, jsonDeserializer);
        }
        return jsonDeserializer;
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) TypeWrappedDeserializer(com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Example 35 with TypeDeserializer

use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project cyclops by aol.

the class IterableXDeserializer method createContextual.

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
    JsonDeserializer<?> deser = this.valueDeserializer;
    TypeDeserializer typeDeser = this.typeDeserializerForValue;
    if (deser == null) {
        deser = ctxt.findContextualValueDeserializer(type.getContentType(), property);
    }
    if (typeDeser != null) {
        typeDeser = typeDeser.forProperty(property);
    }
    return new IterableXDeserializer(elementType, itX, typeDeser, deser, type);
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) AsArrayTypeDeserializer(com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer)

Aggregations

TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)35 IOException (java.io.IOException)10 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)2 TypeWrappedDeserializer (com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer)2 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)2 ObjectBuffer (com.fasterxml.jackson.databind.util.ObjectBuffer)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 Version (com.fasterxml.jackson.core.Version)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DefaultDeserializationContext (com.fasterxml.jackson.databind.deser.DefaultDeserializationContext)1 NullValueProvider (com.fasterxml.jackson.databind.deser.NullValueProvider)1 PropertyBasedCreator (com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator)1 PropertyValueBuffer (com.fasterxml.jackson.databind.deser.impl.PropertyValueBuffer)1 Referring (com.fasterxml.jackson.databind.deser.impl.ReadableObjectId.Referring)1 AsArrayTypeDeserializer (com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1