Java Code Examples for com.fasterxml.jackson.databind.annotation.JsonSerialize#Typing

The following examples show how to use com.fasterxml.jackson.databind.annotation.JsonSerialize#Typing . 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: BasicSerializerFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method to check whether global settings and/or class
 * annotations for the bean class indicate that static typing
 * (declared types)  should be used for properties.
 * (instead of dynamic runtime types).
 * 
 * @since 2.1 (earlier had variant with additional 'property' parameter)
 */
protected boolean usesStaticTyping(SerializationConfig config,
        BeanDescription beanDesc, TypeSerializer typeSer)
{
    /* 16-Aug-2010, tatu: If there is a (value) type serializer, we cannot force
     *    static typing; that would make it impossible to handle expected subtypes
     */
    if (typeSer != null) {
        return false;
    }
    AnnotationIntrospector intr = config.getAnnotationIntrospector();
    JsonSerialize.Typing t = intr.findSerializationTyping(beanDesc.getClassInfo());
    if (t != null && t != JsonSerialize.Typing.DEFAULT_TYPING) {
        return (t == JsonSerialize.Typing.STATIC);
    }
    return config.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
 
Example 2
Source File: ReferenceTypeSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected boolean _useStatic(SerializerProvider provider, BeanProperty property,
        JavaType referredType)
{
    // First: no serializer for `Object.class`, must be dynamic
    if (referredType.isJavaLangObject()) {
        return false;
    }
    // but if type is final, might as well fetch
    if (referredType.isFinal()) { // or should we allow annotation override? (only if requested...)
        return true;
    }
    // also: if indicated by typing, should be considered static
    if (referredType.useStaticType()) {
        return true;
    }
    // if neither, maybe explicit annotation?
    AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if ((intr != null) && (property != null)) {
        Annotated ann = property.getMember();
        if (ann != null) {
            JsonSerialize.Typing t = intr.findSerializationTyping(property.getMember());
            if (t == JsonSerialize.Typing.STATIC) {
                return true;
            }
            if (t == JsonSerialize.Typing.DYNAMIC) {
                return false;
            }
        }
    }
    // and finally, may be forced by global static typing (unlikely...)
    return provider.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
 
Example 3
Source File: OptionSerializer.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
private boolean useStatic(SerializerProvider provider, BeanProperty property, JavaType referredType) {
    // First: no serializer for `Object.class`, must be dynamic
    if (referredType.isJavaLangObject()) {
        return false;
    }
    // but if type is final, might as well fetch
    if (referredType.isFinal()) { // or should we allow annotation override? (only if requested...)
        return true;
    }
    // also: if indicated by typing, should be considered static
    if (referredType.useStaticType()) {
        return true;
    }
    // if neither, maybe explicit annotation?
    AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if ((intr != null) && (property != null)) {
        Annotated ann = property.getMember();
        if (ann != null) {
            JsonSerialize.Typing t = intr.findSerializationTyping(property.getMember());
            if (t == JsonSerialize.Typing.STATIC) {
                return true;
            }
            if (t == JsonSerialize.Typing.DYNAMIC) {
                return false;
            }
        }
    }
    // and finally, may be forced by global static typing (unlikely...)
    return provider.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
 
Example 4
Source File: LazySerializer.java    From vavr-jackson with Apache License 2.0 5 votes vote down vote up
private boolean useStatic(SerializerProvider provider, BeanProperty property, JavaType referredType) {
    // First: no serializer for `Object.class`, must be dynamic
    if (referredType.isJavaLangObject()) {
        return false;
    }
    // but if type is final, might as well fetch
    if (referredType.isFinal()) { // or should we allow annotation override? (only if requested...)
        return true;
    }
    // also: if indicated by typing, should be considered static
    if (referredType.useStaticType()) {
        return true;
    }
    // if neither, maybe explicit annotation?
    AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if ((intr != null) && (property != null)) {
        Annotated ann = property.getMember();
        if (ann != null) {
            JsonSerialize.Typing t = intr.findSerializationTyping(property.getMember());
            if (t == JsonSerialize.Typing.STATIC) {
                return true;
            }
            if (t == JsonSerialize.Typing.DYNAMIC) {
                return false;
            }
        }
    }
    // and finally, may be forced by global static typing (unlikely...)
    return provider.isEnabled(MapperFeature.USE_STATIC_TYPING);
}
 
Example 5
Source File: PropertyBuilder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method that will try to determine statically defined type of property
 * being serialized, based on annotations (for overrides), and alternatively
 * declared type (if static typing for serialization is enabled).
 * If neither can be used (no annotations, dynamic typing), returns null.
 */
protected JavaType findSerializationType(Annotated a, boolean useStaticTyping, JavaType declaredType)
    throws JsonMappingException
{
    JavaType secondary = _annotationIntrospector.refineSerializationType(_config, a, declaredType);

    // 11-Oct-2015, tatu: As of 2.7, not 100% sure following checks are needed. But keeping
    //    for now, just in case
    if (secondary != declaredType) {
        Class<?> serClass = secondary.getRawClass();
        // Must be a super type to be usable
        Class<?> rawDeclared = declaredType.getRawClass();
        if (serClass.isAssignableFrom(rawDeclared)) {
            ; // fine as is
        } else {
            /* 18-Nov-2010, tatu: Related to fixing [JACKSON-416], an issue with such
             *   check is that for deserialization more specific type makes sense;
             *   and for serialization more generic. But alas JAXB uses but a single
             *   annotation to do both... Hence, we must just discard type, as long as
             *   types are related
             */
            if (!rawDeclared.isAssignableFrom(serClass)) {
                throw new IllegalArgumentException("Illegal concrete-type annotation for method '"+a.getName()+"': class "+serClass.getName()+" not a super-type of (declared) class "+rawDeclared.getName());
            }
            /* 03-Dec-2010, tatu: Actually, ugh, we may need to further relax this
             *   and actually accept subtypes too for serialization. Bit dangerous in theory
             *   but need to trust user here...
             */
        }
        useStaticTyping = true;
        declaredType = secondary;
    }
    // If using static typing, declared type is known to be the type...
    JsonSerialize.Typing typing = _annotationIntrospector.findSerializationTyping(a);
    if ((typing != null) && (typing != JsonSerialize.Typing.DEFAULT_TYPING)) {
        useStaticTyping = (typing == JsonSerialize.Typing.STATIC);
    }
    if (useStaticTyping) {
        // 11-Oct-2015, tatu: Make sure JavaType also "knows" static-ness...
        return declaredType.withStaticTyping();
        
    }
    return null;
}
 
Example 6
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JsonSerialize.Typing findSerializationTyping(Annotated a) {
    JsonSerialize.Typing r = _primary.findSerializationTyping(a);
    return (r == null) ? _secondary.findSerializationTyping(a) : r;
}
 
Example 7
Source File: AnnotationIntrospector.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method for accessing declared typing mode annotated (if any).
 * This is used for type detection, unless more granular settings
 * (such as actual exact type; or serializer to use which means
 * no type information is needed) take precedence.
 *
 * @return Typing mode to use, if annotation is found; null otherwise
 */
public JsonSerialize.Typing findSerializationTyping(Annotated a) {
    return null;
}