Search in sources :

Example 1 with ClassKey

use of com.fasterxml.jackson.databind.type.ClassKey in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageModuleProviderTest method testPageModule.

@Test
public void testPageModule() throws Exception {
    PageModuleProvider pmp = new PageModuleProvider();
    Module module = pmp.getModule();
    assertTrue(module instanceof SimpleModule);
    Field field = module.getClass().getDeclaredField("_serializers");
    field.setAccessible(true);
    SimpleSerializers simpleSerializers = (SimpleSerializers) field.get(module);
    field = simpleSerializers.getClass().getDeclaredField("_interfaceMappings");
    field.setAccessible(true);
    HashMap<ClassKey, JsonSerializer<?>> classMappings = (HashMap) field.get(simpleSerializers);
    assertTrue(classMappings.containsKey(new ClassKey(Page.class)));
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ClassKey(com.fasterxml.jackson.databind.type.ClassKey) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.jupiter.api.Test)

Example 2 with ClassKey

use of com.fasterxml.jackson.databind.type.ClassKey in project jackson-databind by FasterXML.

the class BeanDeserializerBase method _findSubclassDeserializer.

/**
     * Helper method called to (try to) locate deserializer for given sub-type of
     * type that this deserializer handles.
     */
protected JsonDeserializer<Object> _findSubclassDeserializer(DeserializationContext ctxt, Object bean, TokenBuffer unknownTokens) throws IOException {
    JsonDeserializer<Object> subDeser;
    // First: maybe we have already created sub-type deserializer?
    synchronized (this) {
        subDeser = (_subDeserializers == null) ? null : _subDeserializers.get(new ClassKey(bean.getClass()));
    }
    if (subDeser != null) {
        return subDeser;
    }
    // If not, maybe we can locate one. First, need provider
    JavaType type = ctxt.constructType(bean.getClass());
    /* 30-Jan-2012, tatu: Ideally we would be passing referring
         *   property; which in theory we could keep track of via
         *   ResolvableDeserializer (if we absolutely must...).
         *   But for now, let's not bother.
         */
    //        subDeser = ctxt.findValueDeserializer(type, _property);
    subDeser = ctxt.findRootValueDeserializer(type);
    // Also, need to cache it
    if (subDeser != null) {
        synchronized (this) {
            if (_subDeserializers == null) {
                _subDeserializers = new HashMap<ClassKey, JsonDeserializer<Object>>();
                ;
            }
            _subDeserializers.put(new ClassKey(bean.getClass()), subDeser);
        }
    }
    return subDeser;
}
Also used : ClassKey(com.fasterxml.jackson.databind.type.ClassKey)

Example 3 with ClassKey

use of com.fasterxml.jackson.databind.type.ClassKey in project jackson-databind by FasterXML.

the class SimpleAbstractTypeResolver method findTypeMapping.

@Override
public JavaType findTypeMapping(DeserializationConfig config, JavaType type) {
    // this is the main mapping base, so let's 
    Class<?> src = type.getRawClass();
    Class<?> dst = _mappings.get(new ClassKey(src));
    if (dst == null) {
        return null;
    }
    // 09-Aug-2015, tatu: Instead of direct call via JavaType, better use TypeFactory
    return config.getTypeFactory().constructSpecializedType(type, dst);
}
Also used : ClassKey(com.fasterxml.jackson.databind.type.ClassKey)

Example 4 with ClassKey

use of com.fasterxml.jackson.databind.type.ClassKey in project jackson-databind by FasterXML.

the class SimpleMixInResolver method setLocalDefinitions.

public void setLocalDefinitions(Map<Class<?>, Class<?>> sourceMixins) {
    if (sourceMixins == null || sourceMixins.isEmpty()) {
        _localMixIns = null;
    } else {
        Map<ClassKey, Class<?>> mixIns = new HashMap<ClassKey, Class<?>>(sourceMixins.size());
        for (Map.Entry<Class<?>, Class<?>> en : sourceMixins.entrySet()) {
            mixIns.put(new ClassKey(en.getKey()), en.getValue());
        }
        _localMixIns = mixIns;
    }
}
Also used : HashMap(java.util.HashMap) ClassKey(com.fasterxml.jackson.databind.type.ClassKey) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ClassKey

use of com.fasterxml.jackson.databind.type.ClassKey in project jackson-databind by FasterXML.

the class SimpleSerializers method findSerializer.

/*
    /**********************************************************
    /* Serializers implementation
    /**********************************************************
     */
@Override
public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
    Class<?> cls = type.getRawClass();
    ClassKey key = new ClassKey(cls);
    JsonSerializer<?> ser = null;
    // First: direct match?
    if (cls.isInterface()) {
        if (_interfaceMappings != null) {
            ser = _interfaceMappings.get(key);
            if (ser != null) {
                return ser;
            }
        }
    } else {
        if (_classMappings != null) {
            ser = _classMappings.get(key);
            if (ser != null) {
                return ser;
            }
            // [Issue#227]: Handle registration of plain `Enum` serializer
            if (_hasEnumSerializer && type.isEnumType()) {
                key.reset(Enum.class);
                ser = _classMappings.get(key);
                if (ser != null) {
                    return ser;
                }
            }
            // If not direct match, maybe super-class match?
            for (Class<?> curr = cls; (curr != null); curr = curr.getSuperclass()) {
                key.reset(curr);
                ser = _classMappings.get(key);
                if (ser != null) {
                    return ser;
                }
            }
        }
    }
    // No direct match? How about super-interfaces?
    if (_interfaceMappings != null) {
        ser = _findInterfaceMapping(cls, key);
        if (ser != null) {
            return ser;
        }
        // still no matches? Maybe interfaces of super classes
        if (!cls.isInterface()) {
            while ((cls = cls.getSuperclass()) != null) {
                ser = _findInterfaceMapping(cls, key);
                if (ser != null) {
                    return ser;
                }
            }
        }
    }
    return null;
}
Also used : ClassKey(com.fasterxml.jackson.databind.type.ClassKey)

Aggregations

ClassKey (com.fasterxml.jackson.databind.type.ClassKey)7 HashMap (java.util.HashMap)3 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)2 Module (com.fasterxml.jackson.databind.Module)2 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)2 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)2 Field (java.lang.reflect.Field)2 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)1 Map (java.util.Map)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1