Search in sources :

Example 21 with SimpleModule

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

the class TestContextualKeyTypes method testSimpleKeyDeser.

/*
    /**********************************************************
    /* Unit tests, deserialization
    /**********************************************************
     */
public void testSimpleKeyDeser() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addKeyDeserializer(String.class, new ContextualDeser("???"));
    mapper.registerModule(module);
    MapBean result = mapper.readValue("{\"map\":{\"a\":3}}", MapBean.class);
    Map<String, Integer> map = result.map;
    assertNotNull(map);
    assertEquals(1, map.size());
    Map.Entry<String, Integer> entry = map.entrySet().iterator().next();
    assertEquals(Integer.valueOf(3), entry.getValue());
    assertEquals("map:a", entry.getKey());
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 22 with SimpleModule

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

the class TestContextualSerialization method testMethodAnnotationInList.

// Serializer should get passed property context even if contained in a Collection.
public void testMethodAnnotationInList() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addSerializer(String.class, new AnnotatedContextualSerializer());
    mapper.registerModule(module);
    ContextualListBean beans = new ContextualListBean("abc");
    assertEquals("{\"beans\":[\"list->abc\"]}", mapper.writeValueAsString(beans));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 23 with SimpleModule

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

the class TestContextualSerialization method testMethodAnnotations.

/*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */
// Test to verify that contextual serializer can make use of property
// (method, field) annotations.
public void testMethodAnnotations() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addSerializer(String.class, new AnnotatedContextualSerializer());
    mapper.registerModule(module);
    assertEquals("{\"value\":\"see:foobar\"}", mapper.writeValueAsString(new ContextualBean("foobar")));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 24 with SimpleModule

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

the class TestUpdateViaObjectReader method testIssue744.

// [databind#744]
public void testIssue744() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(DataA.class, new DataADeserializer());
    mapper.registerModule(module);
    DataB db = new DataB();
    db.da.i = 11;
    db.k = 13;
    String jsonBString = mapper.writeValueAsString(db);
    JsonNode jsonBNode = mapper.valueToTree(db);
    // create parent
    DataB dbNewViaString = mapper.readValue(jsonBString, DataB.class);
    assertEquals(5, dbNewViaString.da.i);
    assertEquals(13, dbNewViaString.k);
    DataB dbNewViaNode = mapper.treeToValue(jsonBNode, DataB.class);
    assertEquals(5, dbNewViaNode.da.i);
    assertEquals(13, dbNewViaNode.k);
    // update parent
    DataB dbUpdViaString = new DataB();
    DataB dbUpdViaNode = new DataB();
    assertEquals(1, dbUpdViaString.da.i);
    assertEquals(3, dbUpdViaString.k);
    mapper.readerForUpdating(dbUpdViaString).readValue(jsonBString);
    assertEquals(5, dbUpdViaString.da.i);
    assertEquals(13, dbUpdViaString.k);
    assertEquals(1, dbUpdViaNode.da.i);
    assertEquals(3, dbUpdViaNode.k);
    mapper.readerForUpdating(dbUpdViaNode).readValue(jsonBNode);
    assertEquals(5, dbUpdViaNode.da.i);
    assertEquals(13, dbUpdViaNode.k);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 25 with SimpleModule

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

the class NullHandlingTest method testAnySetterNulls.

public void testAnySetterNulls() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("test", Version.unknownVersion());
    module.addDeserializer(String.class, new FunnyNullDeserializer());
    mapper.registerModule(module);
    String fieldName = "fieldName";
    String nullValue = "{\"" + fieldName + "\":null}";
    // should get non-default null directly:
    AnySetter result = mapper.readValue(nullValue, AnySetter.class);
    assertEquals(1, result.getAny().size());
    assertNotNull(result.getAny().get(fieldName));
    assertEquals("funny", result.getAny().get(fieldName));
    // as well as via ObjectReader
    ObjectReader reader = mapper.readerFor(AnySetter.class);
    result = reader.readValue(nullValue);
    assertEquals(1, result.getAny().size());
    assertNotNull(result.getAny().get(fieldName));
    assertEquals("funny", result.getAny().get(fieldName));
}
Also used : JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)108 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)39 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 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)4 IOException (java.io.IOException)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonParser (com.fasterxml.jackson.core.JsonParser)3 Module (com.fasterxml.jackson.databind.Module)3 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2