Search in sources :

Example 41 with SimpleModule

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());
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 42 with SimpleModule

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());
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 43 with SimpleModule

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);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 44 with SimpleModule

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);
}
Also used : SimpleSerializers(com.fasterxml.jackson.databind.module.SimpleSerializers) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Activate(org.apache.felix.scr.annotations.Activate)

Example 45 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)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)112 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)41 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 IOException (java.io.IOException)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)3 Module (com.fasterxml.jackson.databind.Module)3 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)3