Java Code Examples for com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder#inclusion()

The following examples show how to use com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder#inclusion() . 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: JsonJacksonCodec.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected void initTypeInclusion(ObjectMapper mapObjectMapper) {
    TypeResolverBuilder<?> mapTyper = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL) {
        public boolean useForType(JavaType t) {
            switch (_appliesFor) {
            case NON_CONCRETE_AND_ARRAYS:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // fall through
            case OBJECT_AND_NON_CONCRETE:
                return (t.getRawClass() == Object.class) || !t.isConcrete();
            case NON_FINAL:
                while (t.isArrayType()) {
                    t = t.getContentType();
                }
                // to fix problem with wrong long to int conversion
                if (t.getRawClass() == Long.class) {
                    return true;
                }
                if (t.getRawClass() == XMLGregorianCalendar.class) {
                    return false;
                }
                return !t.isFinal(); // includes Object.class
            default:
                // case JAVA_LANG_OBJECT:
                return t.getRawClass() == Object.class;
            }
        }
    };
    mapTyper.init(JsonTypeInfo.Id.CLASS, null);
    mapTyper.inclusion(JsonTypeInfo.As.PROPERTY);
    mapObjectMapper.setDefaultTyping(mapTyper);
}
 
Example 2
Source File: JacksonAnnotationIntrospector.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Helper method called to construct and initialize instance of {@link TypeResolverBuilder}
 * if given annotated element indicates one is needed.
 */
@SuppressWarnings("deprecation")
protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
        Annotated ann, JavaType baseType)
{
    // First: maybe we have explicit type resolver?
    TypeResolverBuilder<?> b;
    JsonTypeInfo info = _findAnnotation(ann, JsonTypeInfo.class);
    JsonTypeResolver resAnn = _findAnnotation(ann, JsonTypeResolver.class);
    
    if (resAnn != null) {
        if (info == null) {
            return null;
        }
        /* let's not try to force access override (would need to pass
         * settings through if we did, since that's not doable on some
         * platforms)
         */
        b = config.typeResolverBuilderInstance(ann, resAnn.value());
    } else { // if not, use standard one, if indicated by annotations
        if (info == null) {
            return null;
        }
        // bit special; must return 'marker' to block use of default typing:
        if (info.use() == JsonTypeInfo.Id.NONE) {
            return _constructNoTypeResolverBuilder();
        }
        b = _constructStdTypeResolverBuilder();
    }
    // Does it define a custom type id resolver?
    JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
    TypeIdResolver idRes = (idResInfo == null) ? null
            : config.typeIdResolverInstance(ann, idResInfo.value());
    if (idRes != null) {
        idRes.init(baseType);
    }
    b = b.init(info.use(), idRes);
    /* 13-Aug-2011, tatu: One complication; external id
     *   only works for properties; so if declared for a Class, we will need
     *   to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
     */
    JsonTypeInfo.As inclusion = info.include();
    if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
        inclusion = JsonTypeInfo.As.PROPERTY;
    }
    b = b.inclusion(inclusion);
    b = b.typeProperty(info.property());
    Class<?> defaultImpl = info.defaultImpl();

    // 08-Dec-2014, tatu: To deprecate `JsonTypeInfo.None` we need to use other placeholder(s);
    //   and since `java.util.Void` has other purpose (to indicate "deser as null"), we'll instead
    //   use `JsonTypeInfo.class` itself. But any annotation type will actually do, as they have no
    //   valid use (cannot instantiate as default)
    if (defaultImpl != JsonTypeInfo.None.class && !defaultImpl.isAnnotation()) {
        b = b.defaultImpl(defaultImpl);
    }
    b = b.typeIdVisibility(info.visible());
    return b;
}
 
Example 3
Source File: MarshallerService.java    From dawnsci with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Create a TypeResolverBuilder which will add class id information to JSON-serialized objects to
 * allow the correct classes to be loaded during deserialization.
 * <p>
 * Any IMarshaller-provided serializers / deserializers take precedence over this class identification info.
 * <p>
 * NOTE: this strongly relies on the exact implementation of the Jackson library - it was written to work with
 * version 2.2.0 and has not been tested with any other version.
 *
 * @return the customised TypeResolverBuilder
 * @throws ClassRegistryDuplicateIdException
 * @throws CoreException
 */
private TypeResolverBuilder<?> createRegisteredTypeIdResolver() throws ClassRegistryDuplicateIdException, CoreException {
	MarshallerServiceClassRegistry registry = new MarshallerServiceClassRegistry(extra_registries);
	TypeResolverBuilder<?> typer = new RegisteredTypeResolverBuilder(registry);
	typer = typer.init(JsonTypeInfo.Id.CUSTOM, null);
	typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
	typer = typer.typeProperty(TYPE_INFO_FIELD_NAME);
	return typer;
}