Search in sources :

Example 11 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilder method configure.

/**
	 * 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
	 */
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()));
    }
}
Also used : Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JacksonXmlModule(com.fasterxml.jackson.dataformat.xml.JacksonXmlModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 12 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method modules.

@Test
public void modules() {
    NumberSerializer serializer1 = new NumberSerializer(Integer.class);
    SimpleModule module = new SimpleModule();
    module.addSerializer(Integer.class, serializer1);
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build();
    Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
    assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
Also used : NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project spring-framework by spring-projects.

the class Jackson2ObjectMapperFactoryBeanTests method setModules.

@Test
public void setModules() {
    NumberSerializer serializer = new NumberSerializer(Integer.class);
    SimpleModule module = new SimpleModule();
    module.addSerializer(Integer.class, serializer);
    this.factory.setModules(Arrays.asList(new Module[] { module }));
    this.factory.afterPropertiesSet();
    ObjectMapper objectMapper = this.factory.getObject();
    Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
    assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
}
Also used : NumberSerializer(com.fasterxml.jackson.databind.ser.std.NumberSerializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Serializers(com.fasterxml.jackson.databind.ser.Serializers) SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project titan by thinkaurelius.

the class GeoshapeTest method testGeoJsonSerialization.

@Test
public void testGeoJsonSerialization() throws IOException {
    SimpleModule module = new SimpleModule();
    module.addSerializer(new Geoshape.GeoshapeGsonSerializer());
    final ObjectMapper om = new ObjectMapper();
    om.registerModule(module);
    assertEquals("{\"type\":\"Point\",\"coordinates\":[20.5,10.5]}", om.writeValueAsString(Geoshape.point(10.5, 20.5)));
    assertEquals("{\"type\":\"Polygon\",\"coordinates\":[[20.5,10.5],[22.5,10.5],[22.5,12.5],[20.5,12.5]]}", om.writeValueAsString(Geoshape.box(10.5, 20.5, 12.5, 22.5)));
    assertEquals("{\"type\":\"Circle\",\"radius\":30.5,\"coordinates\":[20.5,10.5]}", om.writeValueAsString(Geoshape.circle(10.5, 20.5, 30.5)));
}
Also used : Geoshape(com.thinkaurelius.titan.core.attribute.Geoshape) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project Gaffer by gchq.

the class HyperLogLogPlusJsonSerialisationTest method before.

@Before
public void before() {
    mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
    final SimpleModule module = new SimpleModule(HyperLogLogPlusJsonConstants.HYPER_LOG_LOG_PLUS_SERIALISER_MODULE_NAME, new Version(1, 0, 0, null));
    module.addSerializer(HyperLogLogPlus.class, new HyperLogLogPlusJsonSerialiser());
    module.addDeserializer(HyperLogLogPlus.class, new HyperLogLogPlusJsonDeserialiser());
    mapper.registerModule(module);
}
Also used : Version(com.fasterxml.jackson.core.Version) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Before(org.junit.Before)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)108 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)39 Test (org.junit.Test)13 Version (com.fasterxml.jackson.core.Version)8 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)8 Test (org.testng.annotations.Test)8 TestingTypeDeserializer (com.facebook.presto.spi.type.TestingTypeDeserializer)7 TestingTypeManager (com.facebook.presto.spi.type.TestingTypeManager)7 Type (com.facebook.presto.spi.type.Type)7 Block (com.facebook.presto.spi.block.Block)6 TestingBlockEncodingSerde (com.facebook.presto.spi.block.TestingBlockEncodingSerde)6 TestingBlockJsonSerde (com.facebook.presto.spi.block.TestingBlockJsonSerde)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)4 IOException (java.io.IOException)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonParser (com.fasterxml.jackson.core.JsonParser)3 Module (com.fasterxml.jackson.databind.Module)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2