Example usage for com.fasterxml.jackson.databind.jsontype TypeResolverBuilder inclusion

List of usage examples for com.fasterxml.jackson.databind.jsontype TypeResolverBuilder inclusion

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.jsontype TypeResolverBuilder inclusion.

Prototype

public abstract T inclusion(JsonTypeInfo.As paramAs);

Source Link

Usage

From source file:org.carewebframework.common.JSONUtil.java

/**
 * Initializes a mapper in a thread-safe way.
 * //  ww w . j  a  v  a  2 s .  com
 * @return The initialized mapper.
 */
private static ObjectMapper initMapper(String typeProperty) {
    synchronized (mappers) {
        ObjectMapper mapper = mappers.get(typeProperty);
        if (mapper == null) {
            mapper = new ObjectMapper();
            TypeResolverBuilder<?> typer = new CWTypeResolverBuilder();
            typer = typer.init(JsonTypeInfo.Id.CUSTOM, new CWTypedIdResolver(mapper));
            typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
            typer = typer.typeProperty(typeProperty);
            mapper.setDefaultTyping(typer);
            mappers.put(typeProperty, mapper);
        }

        return mapper;
    }
}

From source file:org.redisson.codec.JsonJacksonCodec.java

public JsonJacksonCodec() {
    createObjectMapper(objectMapper);/*w  w  w.j a  v  a2 s. com*/
    TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL);
    typer.init(JsonTypeInfo.Id.CLASS, null);
    typer.inclusion(JsonTypeInfo.As.PROPERTY);
    objectMapper.setDefaultTyping(typer);

    createObjectMapper(mapObjectMapper);
    // type info inclusion
    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;
                }
                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);
}

From source file:org.springframework.security.jackson2.SecurityJackson2Modules.java

/**
 * Creates a TypeResolverBuilder that performs whitelisting.
 * @return a TypeResolverBuilder that performs whitelisting.
 *//*from w  w  w  .  j  a va 2s  .  c o m*/
private static TypeResolverBuilder<? extends TypeResolverBuilder> createWhitelistedDefaultTyping() {
    TypeResolverBuilder<? extends TypeResolverBuilder> result = new WhitelistTypeResolverBuilder(
            ObjectMapper.DefaultTyping.NON_FINAL);
    result = result.init(JsonTypeInfo.Id.CLASS, null);
    result = result.inclusion(JsonTypeInfo.As.PROPERTY);
    return result;
}