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()));
}
}
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));
}
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));
}
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)));
}
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);
}
Aggregations