Search in sources :

Example 26 with TypeDeserializer

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

the class BasicDeserializerFactory method createArrayDeserializer.

/*
    /**********************************************************
    /* JsonDeserializerFactory impl: array deserializers
    /**********************************************************
     */
@Override
public JsonDeserializer<?> createArrayDeserializer(DeserializationContext ctxt, ArrayType type, final BeanDescription beanDesc) throws JsonMappingException {
    final DeserializationConfig config = ctxt.getConfig();
    JavaType elemType = type.getContentType();
    // Very first thing: is deserializer hard-coded for elements?
    JsonDeserializer<Object> contentDeser = elemType.getValueHandler();
    // Then optional type info: if type has been resolved, we may already know type deserializer:
    TypeDeserializer elemTypeDeser = elemType.getTypeHandler();
    // but if not, may still be possible to find:
    if (elemTypeDeser == null) {
        elemTypeDeser = findTypeDeserializer(config, elemType);
    }
    // 23-Nov-2010, tatu: Custom array deserializer?
    JsonDeserializer<?> deser = _findCustomArrayDeserializer(type, config, beanDesc, elemTypeDeser, contentDeser);
    if (deser == null) {
        if (contentDeser == null) {
            Class<?> raw = elemType.getRawClass();
            if (elemType.isPrimitive()) {
                return PrimitiveArrayDeserializers.forType(raw);
            } else if (raw == String.class) {
                return StringArrayDeserializer.instance;
            }
        }
        deser = new ObjectArrayDeserializer(type, contentDeser, elemTypeDeser);
    }
    // and then new with 2.2: ability to post-process it too (Issue#120)
    if (_factoryConfig.hasDeserializerModifiers()) {
        for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
            deser = mod.modifyArrayDeserializer(config, type, beanDesc, deser);
        }
    }
    return deser;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 27 with TypeDeserializer

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

the class BasicDeserializerFactory method createMapDeserializer.

/*
    /**********************************************************
    /* JsonDeserializerFactory impl: Map(-like) deserializers
    /**********************************************************
     */
@Override
public JsonDeserializer<?> createMapDeserializer(DeserializationContext ctxt, MapType type, BeanDescription beanDesc) throws JsonMappingException {
    final DeserializationConfig config = ctxt.getConfig();
    JavaType keyType = type.getKeyType();
    JavaType contentType = type.getContentType();
    // First: is there annotation-specified deserializer for values?
    @SuppressWarnings("unchecked") JsonDeserializer<Object> contentDeser = (JsonDeserializer<Object>) contentType.getValueHandler();
    // Ok: need a key deserializer (null indicates 'default' here)
    KeyDeserializer keyDes = (KeyDeserializer) keyType.getValueHandler();
    // Then optional type info; either attached to type, or resolved separately:
    TypeDeserializer contentTypeDeser = contentType.getTypeHandler();
    // but if not, may still be possible to find:
    if (contentTypeDeser == null) {
        contentTypeDeser = findTypeDeserializer(config, contentType);
    }
    // 23-Nov-2010, tatu: Custom deserializer?
    JsonDeserializer<?> deser = _findCustomMapDeserializer(type, config, beanDesc, keyDes, contentTypeDeser, contentDeser);
    if (deser == null) {
        // Value handling is identical for all, but EnumMap requires special handling for keys
        Class<?> mapClass = type.getRawClass();
        if (EnumMap.class.isAssignableFrom(mapClass)) {
            ValueInstantiator inst;
            //    custom sub-classes, see [databind#1544]
            if (mapClass == EnumMap.class) {
                inst = null;
            } else {
                inst = findValueInstantiator(ctxt, beanDesc);
            }
            Class<?> kt = keyType.getRawClass();
            if (kt == null || !kt.isEnum()) {
                throw new IllegalArgumentException("Can not construct EnumMap; generic (key) type not available");
            }
            deser = new EnumMapDeserializer(type, inst, null, contentDeser, contentTypeDeser, null);
        }
        /* But there is one more twist: if we are being asked to instantiate
             * an interface or abstract Map, we need to either find something
             * that implements the thing, or give up.
             *
             * Note that we do NOT try to guess based on secondary interfaces
             * here; that would probably not work correctly since casts would
             * fail later on (as the primary type is not the interface we'd
             * be implementing)
             */
        if (deser == null) {
            if (type.isInterface() || type.isAbstract()) {
                @SuppressWarnings("rawtypes") Class<? extends Map> fallback = _mapFallbacks.get(mapClass.getName());
                if (fallback != null) {
                    mapClass = fallback;
                    type = (MapType) config.constructSpecializedType(type, mapClass);
                    // But if so, also need to re-check creators...
                    beanDesc = config.introspectForCreation(type);
                } else {
                    // [databind#292]: Actually, may be fine, but only if polymorphic deser enabled
                    if (type.getTypeHandler() == null) {
                        throw new IllegalArgumentException("Can not find a deserializer for non-concrete Map type " + type);
                    }
                    deser = AbstractDeserializer.constructForNonPOJO(beanDesc);
                }
            }
            if (deser == null) {
                ValueInstantiator inst = findValueInstantiator(ctxt, beanDesc);
                // 01-May-2016, tatu: Which base type to use here gets tricky, since
                //   most often it ought to be `Map` or `EnumMap`, but due to abstract
                //   mapping it will more likely be concrete type like `HashMap`.
                //   So, for time being, just pass `Map.class`
                MapDeserializer md = new MapDeserializer(type, inst, keyDes, contentDeser, contentTypeDeser);
                JsonIgnoreProperties.Value ignorals = config.getDefaultPropertyIgnorals(Map.class, beanDesc.getClassInfo());
                Set<String> ignored = (ignorals == null) ? null : ignorals.findIgnoredForDeserialization();
                md.setIgnorableProperties(ignored);
                deser = md;
            }
        }
    }
    if (_factoryConfig.hasDeserializerModifiers()) {
        for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
            deser = mod.modifyMapDeserializer(config, type, beanDesc, deser);
        }
    }
    return deser;
}
Also used : JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 28 with TypeDeserializer

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

the class BasicDeserializerFactory method findDefaultDeserializer.

/**
     * Helper method called to find one of default serializers for "well-known"
     * platform types: JDK-provided types, and small number of public Jackson
     * API types.
     * 
     * @since 2.2
     */
public JsonDeserializer<?> findDefaultDeserializer(DeserializationContext ctxt, JavaType type, BeanDescription beanDesc) throws JsonMappingException {
    Class<?> rawType = type.getRawClass();
    // Object ("untyped"), String equivalents:
    if (rawType == CLASS_OBJECT) {
        // 11-Feb-2015, tatu: As per [databind#700] need to be careful wrt non-default Map, List.
        DeserializationConfig config = ctxt.getConfig();
        JavaType lt, mt;
        if (_factoryConfig.hasAbstractTypeResolvers()) {
            lt = _findRemappedType(config, List.class);
            mt = _findRemappedType(config, Map.class);
        } else {
            lt = mt = null;
        }
        return new UntypedObjectDeserializer(lt, mt);
    }
    if (rawType == CLASS_STRING || rawType == CLASS_CHAR_BUFFER) {
        return StringDeserializer.instance;
    }
    if (rawType == CLASS_ITERABLE) {
        // [databind#199]: Can and should 'upgrade' to a Collection type:
        TypeFactory tf = ctxt.getTypeFactory();
        JavaType[] tps = tf.findTypeParameters(type, CLASS_ITERABLE);
        JavaType elemType = (tps == null || tps.length != 1) ? TypeFactory.unknownType() : tps[0];
        CollectionType ct = tf.constructCollectionType(Collection.class, elemType);
        // Should we re-introspect beanDesc? For now let's not...
        return createCollectionDeserializer(ctxt, ct, beanDesc);
    }
    if (rawType == CLASS_MAP_ENTRY) {
        // 28-Apr-2015, tatu: TypeFactory does it all for us already so
        JavaType kt = type.containedTypeOrUnknown(0);
        JavaType vt = type.containedTypeOrUnknown(1);
        TypeDeserializer vts = (TypeDeserializer) vt.getTypeHandler();
        if (vts == null) {
            vts = findTypeDeserializer(ctxt.getConfig(), vt);
        }
        JsonDeserializer<Object> valueDeser = vt.getValueHandler();
        KeyDeserializer keyDes = (KeyDeserializer) kt.getValueHandler();
        return new MapEntryDeserializer(type, keyDes, valueDeser, vts);
    }
    String clsName = rawType.getName();
    if (rawType.isPrimitive() || clsName.startsWith("java.")) {
        // Primitives/wrappers, other Numbers:
        JsonDeserializer<?> deser = NumberDeserializers.find(rawType, clsName);
        if (deser == null) {
            deser = DateDeserializers.find(rawType, clsName);
        }
        if (deser != null) {
            return deser;
        }
    }
    // and a few Jackson types as well:
    if (rawType == TokenBuffer.class) {
        return new TokenBufferDeserializer();
    }
    JsonDeserializer<?> deser = findOptionalStdDeserializer(ctxt, type, beanDesc);
    if (deser != null) {
        return deser;
    }
    return JdkDeserializers.find(rawType, clsName);
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 29 with TypeDeserializer

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

the class BasicDeserializerFactory method createMapLikeDeserializer.

// Copied almost verbatim from "createMapDeserializer" -- should try to share more code
@Override
public JsonDeserializer<?> createMapLikeDeserializer(DeserializationContext ctxt, MapLikeType type, final BeanDescription beanDesc) throws JsonMappingException {
    JavaType keyType = type.getKeyType();
    JavaType contentType = type.getContentType();
    final DeserializationConfig config = ctxt.getConfig();
    // First: is there annotation-specified deserializer for values?
    @SuppressWarnings("unchecked") JsonDeserializer<Object> contentDeser = (JsonDeserializer<Object>) contentType.getValueHandler();
    // Ok: need a key deserializer (null indicates 'default' here)
    KeyDeserializer keyDes = (KeyDeserializer) keyType.getValueHandler();
    /* !!! 24-Jan-2012, tatu: NOTE: impls MUST use resolve() to find key deserializer!
        if (keyDes == null) {
            keyDes = p.findKeyDeserializer(config, keyType, property);
        }
        */
    // Then optional type info (1.5); either attached to type, or resolve separately:
    TypeDeserializer contentTypeDeser = contentType.getTypeHandler();
    // but if not, may still be possible to find:
    if (contentTypeDeser == null) {
        contentTypeDeser = findTypeDeserializer(config, contentType);
    }
    JsonDeserializer<?> deser = _findCustomMapLikeDeserializer(type, config, beanDesc, keyDes, contentTypeDeser, contentDeser);
    if (deser != null) {
        // and then new with 2.2: ability to post-process it too (Issue#120)
        if (_factoryConfig.hasDeserializerModifiers()) {
            for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
                deser = mod.modifyMapLikeDeserializer(config, type, beanDesc, deser);
            }
        }
    }
    return deser;
}
Also used : TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer)

Example 30 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)

Aggregations

TypeDeserializer (com.fasterxml.jackson.databind.jsontype.TypeDeserializer)30 IOException (java.io.IOException)9 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 ObjectBuffer (com.fasterxml.jackson.databind.util.ObjectBuffer)2 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 TypeWrappedDeserializer (com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer)1 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)1