use of com.fasterxml.jackson.databind.module.SimpleModule in project logging-log4j2 by apache.
the class JacksonIssue429MyNamesTest method testStackTraceElementWithCustom.
@Test
public void testStackTraceElementWithCustom() throws Exception {
// first, via bean that contains StackTraceElement
final StackTraceBean bean = MAPPER.readValue(aposToQuotes("{'Location':{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':13}}"), StackTraceBean.class);
Assert.assertNotNull(bean);
Assert.assertNotNull(bean.location);
Assert.assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
// and then directly, iff registered
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addDeserializer(StackTraceElement.class, new MyStackTraceElementDeserializer());
mapper.registerModule(module);
final StackTraceElement elem = mapper.readValue(aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':13}"), StackTraceElement.class);
Assert.assertNotNull(elem);
Assert.assertEquals(StackTraceBean.NUM, elem.getLineNumber());
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project logging-log4j2 by apache.
the class JacksonIssue429Test method testStackTraceElementWithCustom.
@Test
public void testStackTraceElementWithCustom() throws Exception {
// first, via bean that contains StackTraceElement
final StackTraceBean bean = MAPPER.readValue(aposToQuotes("{'Location':'foobar'}"), StackTraceBean.class);
Assert.assertNotNull(bean);
Assert.assertNotNull(bean.location);
Assert.assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
// and then directly, iff registered
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule();
module.addDeserializer(StackTraceElement.class, new Jackson429StackTraceElementDeserializer());
mapper.registerModule(module);
final StackTraceElement elem = mapper.readValue(aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}"), StackTraceElement.class);
Assert.assertNotNull(elem);
Assert.assertEquals(StackTraceBean.NUM, elem.getLineNumber());
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project logging-log4j2 by apache.
the class StackTraceElementMixInTest method testFromJsonWithLog4jModule.
@Test
public void testFromJsonWithLog4jModule() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final boolean encodeThreadContextAsList = false;
final SimpleModule module = new Log4jJsonModule(encodeThreadContextAsList, true);
module.addDeserializer(StackTraceElement.class, new Log4jStackTraceElementDeserializer());
mapper.registerModule(module);
final StackTraceElement expected = new StackTraceElement("package.SomeClass", "someMethod", "SomeClass.java", 123);
final String s = this.aposToQuotes("{'class':'package.SomeClass','method':'someMethod','file':'SomeClass.java','line':123}");
final StackTraceElement actual = mapper.readValue(s, StackTraceElement.class);
Assert.assertEquals(expected, actual);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project sling by apache.
the class ResourceModuleProvider method activate.
@Activate
private void activate(Map<String, Object> props) {
final int maxRecursionLevels = PropertiesUtil.toInteger(props.get(PROP_MAX_RECURSION_LEVELS), DEFAULT_MAX_RECURSION_LEVELS);
this.moduleInstance = new SimpleModule();
SimpleSerializers serializers = new SimpleSerializers();
serializers.addSerializer(Resource.class, new ResourceSerializer(maxRecursionLevels));
moduleInstance.setSerializers(serializers);
}
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()));
}
}
Aggregations