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());
}
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));
}
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")));
}
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);
}
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));
}
Aggregations