Search in sources :

Example 61 with SimpleModule

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

the class JDKStringLikeTypesTest method testStackTraceElementWithCustom.

// [databind#429]
public void testStackTraceElementWithCustom() throws Exception {
    // first, via bean that contains StackTraceElement
    StackTraceBean bean = MAPPER.readValue(aposToQuotes("{'Location':'foobar'}"), StackTraceBean.class);
    assertNotNull(bean);
    assertNotNull(bean.location);
    assertEquals(StackTraceBean.NUM, bean.location.getLineNumber());
    // and then directly, iff registered
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(StackTraceElement.class, new MyStackTraceElementDeserializer());
    mapper.registerModule(module);
    StackTraceElement elem = mapper.readValue("123", StackTraceElement.class);
    assertNotNull(elem);
    assertEquals(StackTraceBean.NUM, elem.getLineNumber());
    // and finally, even as part of real exception
    IOException ioe = mapper.readValue(aposToQuotes("{'stackTrace':[ 123, 456 ]}"), IOException.class);
    assertNotNull(ioe);
    StackTraceElement[] traces = ioe.getStackTrace();
    assertNotNull(traces);
    assertEquals(2, traces.length);
    assertEquals(StackTraceBean.NUM, traces[0].getLineNumber());
    assertEquals(StackTraceBean.NUM, traces[1].getLineNumber());
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 62 with SimpleModule

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

the class TestArrayDeserialization method testCustomDeserializers.

/*
    /**********************************************************
    /* And custom deserializers too
    /**********************************************************
     */
public void testCustomDeserializers() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule testModule = new SimpleModule("test", Version.unknownVersion());
    testModule.addDeserializer(NonDeserializable[].class, new CustomNonDeserArrayDeserializer());
    mapper.registerModule(testModule);
    NonDeserializable[] result = mapper.readValue("[\"a\"]", NonDeserializable[].class);
    assertNotNull(result);
    assertEquals(1, result.length);
    assertEquals("a", result[0].value);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 63 with SimpleModule

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

the class TestCustomDeserializers method testJsonNodeDelegating.

// [databind#623]
public void testJsonNodeDelegating() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(Immutable.class, new StdNodeBasedDeserializer<Immutable>(Immutable.class) {

        @Override
        public Immutable convert(JsonNode root, DeserializationContext ctxt) throws IOException {
            int x = root.path("x").asInt();
            int y = root.path("y").asInt();
            return new Immutable(x, y);
        }
    });
    mapper.registerModule(module);
    Immutable imm = mapper.readValue("{\"x\":-10,\"y\":3}", Immutable.class);
    assertEquals(-10, imm.x);
    assertEquals(3, imm.y);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 64 with SimpleModule

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

the class TestCustomDeserializers method testContextReadValue.

// [#337]: convenience methods for custom deserializers to use
public void testContextReadValue() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(Bean375Outer.class, new Bean375OuterDeserializer());
    module.addDeserializer(Bean375Inner.class, new Bean375InnerDeserializer());
    mapper.registerModule(module);
    // First, without property; doubles up value:
    Bean375Outer outer = mapper.readValue("13", Bean375Outer.class);
    assertEquals(26, outer.inner.x);
    // then with property; should find annotation, turn negative
    Bean375Wrapper w = mapper.readValue("{\"value\":13}", Bean375Wrapper.class);
    assertNotNull(w.value);
    assertNotNull(w.value.inner);
    assertEquals(-13, w.value.inner.x);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 65 with SimpleModule

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

the class NullHandlingTest method testMapOfNulls.

// Test for [#407]
public void testMapOfNulls() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(String.class, new FunnyNullDeserializer());
    mapper.registerModule(module);
    JavaType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class);
    // should get non-default null directly:
    Map<?, ?> deser = mapper.readValue("{\"key\":null}", type);
    assertNotNull(deser);
    assertEquals(1, deser.size());
    assertEquals("funny", deser.get("key"));
    // as well as via ObjectReader
    ObjectReader reader = mapper.readerFor(type);
    deser = reader.readValue("{\"key\":null}");
    assertNotNull(deser);
    assertEquals(1, deser.size());
    assertEquals("funny", deser.get("key"));
}
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