Search in sources :

Example 36 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-module-afterburner by FasterXML.

the class TestStdDeserializerOverrides method testStringDeserOverideNoAfterburner.

public void testStringDeserOverideNoAfterburner() throws Exception {
    final String json = "{\"field\": \"value & value\"}";
    final String EXP = "value & value";
    Issue59Bean resultVanilla = new ObjectMapper().registerModule(new SimpleModule("module", Version.unknownVersion()).addDeserializer(String.class, new DeAmpDeserializer())).readValue(json, Issue59Bean.class);
    assertEquals(EXP, resultVanilla.field);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 37 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-module-afterburner by FasterXML.

the class TestStdSerializerOverrides method testStringSerOverideNoAfterburner.

public void testStringSerOverideNoAfterburner() throws Exception {
    final FooBean input = new FooBean();
    final String EXP = "{\"field\":\"Foo:value\"}";
    String json = new ObjectMapper().registerModule(new SimpleModule("module", Version.unknownVersion()).addSerializer(String.class, new MyStringSerializer())).writeValueAsString(input);
    assertEquals(EXP, json);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 38 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestBeanSerializer method testEmptyBean539.

// [Issue#539]
public void testEmptyBean539() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new SimpleModule("test", Version.unknownVersion()) {

        @Override
        public void setupModule(SetupContext context) {
            super.setupModule(context);
            context.addBeanSerializerModifier(new EmptyBeanModifier539());
        }
    });
    String json = mapper.writeValueAsString(new EmptyBean());
    assertEquals("42", json);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 39 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestCustomSerializers method testDelegating.

// [databind#87]: delegating serializer
public void testDelegating() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addSerializer(new StdDelegatingSerializer(Immutable.class, new StdConverter<Immutable, Map<String, Integer>>() {

        @Override
        public Map<String, Integer> convert(Immutable value) {
            HashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
            map.put("x", value.x());
            map.put("y", value.y());
            return map;
        }
    }));
    mapper.registerModule(module);
    assertEquals("{\"x\":3,\"y\":7}", mapper.writeValueAsString(new Immutable()));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) StdDelegatingSerializer(com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer) StdConverter(com.fasterxml.jackson.databind.util.StdConverter)

Example 40 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestCustomSerializers method testWithCustomElements.

public void testWithCustomElements() throws Exception {
    // First variant that uses per-property override
    StringListWrapper wr = new StringListWrapper("a", null, "b");
    assertEquals(aposToQuotes("{'list':['A',null,'B']}"), MAPPER.writeValueAsString(wr));
    // and then per-type registration
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addSerializer(String.class, new UCStringSerializer());
    ObjectMapper mapper = new ObjectMapper().registerModule(module);
    assertEquals(quote("FOOBAR"), mapper.writeValueAsString("foobar"));
    assertEquals(aposToQuotes("['FOO',null]"), mapper.writeValueAsString(new String[] { "foo", null }));
    List<String> list = Arrays.asList("foo", null);
    assertEquals(aposToQuotes("['FOO',null]"), mapper.writeValueAsString(list));
    Set<String> set = new LinkedHashSet<String>(Arrays.asList("foo", null));
    assertEquals(aposToQuotes("['FOO',null]"), mapper.writeValueAsString(set));
}
Also used : 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