use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualDeserialization method testSimple.
/*
/**********************************************************
/* Unit tests
/**********************************************************
*/
public void testSimple() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(StringValue.class, new MyContextualDeserializer());
mapper.registerModule(module);
ContextualBean bean = mapper.readValue("{\"a\":\"1\",\"b\":\"2\"}", ContextualBean.class);
assertEquals("a=1", bean.a.value);
assertEquals("b=2", bean.b.value);
// try again, to ensure caching etc works
bean = mapper.readValue("{\"a\":\"3\",\"b\":\"4\"}", ContextualBean.class);
assertEquals("a=3", bean.a.value);
assertEquals("b=4", bean.b.value);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualDeserialization method _mapperWithAnnotatedContextual.
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private ObjectMapper _mapperWithAnnotatedContextual() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addDeserializer(StringValue.class, new AnnotatedContextualDeserializer());
mapper.registerModule(module);
return mapper;
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualKeyTypes method testSimpleKeySer.
/*
/**********************************************************
/* Unit tests, serialization
/**********************************************************
*/
public void testSimpleKeySer() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addKeySerializer(String.class, new ContextualKeySerializer("prefix"));
mapper.registerModule(module);
Map<String, Object> input = new HashMap<String, Object>();
input.put("a", Integer.valueOf(3));
String json = mapper.writerFor(TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, Object.class)).writeValueAsString(input);
assertEquals("{\"prefix:a\":3}", json);
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualSerialization method testWrappedBean.
public void testWrappedBean() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(String.class, new AnnotatedContextualSerializer());
mapper.registerModule(module);
assertEquals("{\"wrapped\":{\"value\":\"see:xyz\"}}", mapper.writeValueAsString(new ContextualBeanWrapper("xyz")));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualSerialization method testClassAnnotations.
// Test to verify that contextual serializer can also use annotations
// for enclosing class.
public void testClassAnnotations() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(String.class, new AnnotatedContextualSerializer());
mapper.registerModule(module);
assertEquals("{\"value\":\"Voila->xyz\"}", mapper.writeValueAsString(new BeanWithClassConfig("xyz")));
}
Aggregations