Example usage for com.fasterxml.jackson.databind ObjectMapper setDefaultTyping

List of usage examples for com.fasterxml.jackson.databind ObjectMapper setDefaultTyping

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper setDefaultTyping.

Prototype

public ObjectMapper setDefaultTyping(TypeResolverBuilder<?> typer) 

Source Link

Document

Method for enabling automatic inclusion of type information, using specified handler object for determining which types this affects, as well as details of how information is embedded.

Usage

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

/**
 * Initializes a mapper in a thread-safe way.
 * /*w w  w  .  j  a v a 2s  .  c  o  m*/
 * @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:com.ebuddy.cassandra.cql.dao.CqlStructuredDataSupportSystemTest.java

@SuppressWarnings("unchecked")
@Test(groups = { "system" })
public void convertValueShouldRetainOrderingInMaps() throws Exception {
    SortedMap<String, String> map = new TreeMap<String, String>();
    map.put("b", "1");
    map.put("a", "2");

    ObjectMapper mapper = new ObjectMapper();
    mapper.setDefaultTyping(new CustomTypeResolverBuilder());
    Object converted = mapper.convertValue(map, Object.class);
    assertTrue(converted instanceof LinkedHashMap);
    // keys are sorted by Cassandra as UTF8
    assertEquals(((Map<String, String>) converted).keySet().iterator().next(), "a");
}

From source file:org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.java

/**
 * Configure an existing {@link ObjectMapper} instance with this builder's
 * settings. This can be applied to any number of {@code ObjectMappers}.
 * @param objectMapper the ObjectMapper to configure
 */// ww  w .  j av a  2  s  .c om
public void configure(ObjectMapper objectMapper) {
    Assert.notNull(objectMapper, "ObjectMapper must not be null");

    if (this.findModulesViaServiceLoader) {
        // Jackson 2.2+
        objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader));
    } else if (this.findWellKnownModules) {
        registerWellKnownModulesIfAvailable(objectMapper);
    }

    if (this.modules != null) {
        for (Module module : this.modules) {
            // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules
            objectMapper.registerModule(module);
        }
    }
    if (this.moduleClasses != null) {
        for (Class<? extends Module> module : this.moduleClasses) {
            objectMapper.registerModule(BeanUtils.instantiateClass(module));
        }
    }

    if (this.dateFormat != null) {
        objectMapper.setDateFormat(this.dateFormat);
    }
    if (this.locale != null) {
        objectMapper.setLocale(this.locale);
    }
    if (this.timeZone != null) {
        objectMapper.setTimeZone(this.timeZone);
    }

    if (this.annotationIntrospector != null) {
        objectMapper.setAnnotationIntrospector(this.annotationIntrospector);
    }
    if (this.propertyNamingStrategy != null) {
        objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy);
    }
    if (this.defaultTyping != null) {
        objectMapper.setDefaultTyping(this.defaultTyping);
    }
    if (this.serializationInclusion != null) {
        objectMapper.setSerializationInclusion(this.serializationInclusion);
    }

    if (this.filters != null) {
        objectMapper.setFilterProvider(this.filters);
    }

    for (Class<?> target : this.mixIns.keySet()) {
        objectMapper.addMixIn(target, this.mixIns.get(target));
    }

    if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) {
        SimpleModule module = new SimpleModule();
        addSerializers(module);
        addDeserializers(module);
        objectMapper.registerModule(module);
    }

    customizeDefaultFeatures(objectMapper);
    for (Object feature : this.features.keySet()) {
        configureFeature(objectMapper, feature, this.features.get(feature));
    }

    if (this.handlerInstantiator != null) {
        objectMapper.setHandlerInstantiator(this.handlerInstantiator);
    } else if (this.applicationContext != null) {
        objectMapper.setHandlerInstantiator(
                new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory()));
    }
}

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

public static void enableDefaultTyping(ObjectMapper mapper) {
    if (mapper != null) {
        TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
        if (typeBuilder == null) {
            mapper.setDefaultTyping(createWhitelistedDefaultTyping());
        }/*from  w  w w.j a v a 2  s.  co m*/
    }
}