Search in sources :

Example 26 with SimpleModule

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

the class NullHandlingTest method testListOfNulls.

// [databind#407]
public void testListOfNulls() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(String.class, new FunnyNullDeserializer());
    mapper.registerModule(module);
    List<String> list = Arrays.asList("funny");
    JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, String.class);
    // should get non-default null directly:
    List<?> deser = mapper.readValue("[null]", type);
    assertNotNull(deser);
    assertEquals(1, deser.size());
    assertEquals(list.get(0), deser.get(0));
    // as well as via ObjectReader
    ObjectReader reader = mapper.readerFor(type);
    deser = reader.readValue("[null]");
    assertNotNull(deser);
    assertEquals(1, deser.size());
    assertEquals(list.get(0), deser.get(0));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 27 with SimpleModule

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

the class NullHandlingTest method testCustomRootNulls.

public void testCustomRootNulls() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(String.class, new FunnyNullDeserializer());
    mapper.registerModule(module);
    // should get non-default null directly:
    String str = mapper.readValue("null", String.class);
    assertNotNull(str);
    assertEquals("funny", str);
    // as well as via ObjectReader
    ObjectReader reader = mapper.readerFor(String.class);
    str = reader.readValue("null");
    assertNotNull(str);
    assertEquals("funny", str);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 28 with SimpleModule

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

the class TestCustomDeserializers method testDelegating.

// [Issue#87]: delegating deserializer
public void testDelegating() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(Immutable.class, new StdDelegatingDeserializer<Immutable>(new StdConverter<JsonNode, Immutable>() {

        @Override
        public Immutable convert(JsonNode value) {
            int x = value.path("x").asInt();
            int y = value.path("y").asInt();
            return new Immutable(x, y);
        }
    }));
    mapper.registerModule(module);
    Immutable imm = mapper.readValue("{\"x\":3,\"y\":7}", Immutable.class);
    assertEquals(3, imm.x);
    assertEquals(7, imm.y);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) StdConverter(com.fasterxml.jackson.databind.util.StdConverter)

Example 29 with SimpleModule

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

the class SimpleModuleTest method testSimpleBeanSerializer.

/*
    /**********************************************************
    /* Unit tests; simple serializers
    /**********************************************************
     */
public void testSimpleBeanSerializer() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    mod.addSerializer(new CustomBeanSerializer());
    mapper.registerModule(mod);
    assertEquals(quote("abcde|5"), mapper.writeValueAsString(new CustomBean("abcde", 5)));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 30 with SimpleModule

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

the class SimpleModuleTest method testSimpleEnumSerializer.

public void testSimpleEnumSerializer() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    mod.addSerializer(new SimpleEnumSerializer());
    // for fun, call "multi-module" registration
    mapper.registerModules(mod);
    assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
}
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