use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualSerialization method testMethodAnnotationInMap.
// Serializer should get passed property context even if contained in a Collection.
public void testMethodAnnotationInMap() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(String.class, new AnnotatedContextualSerializer());
mapper.registerModule(module);
ContextualMapBean map = new ContextualMapBean();
map.beans.put("first", "In Map");
assertEquals("{\"beans\":{\"first\":\"map->In Map\"}}", mapper.writeValueAsString(map));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestContextualSerialization method testMethodAnnotationInArray.
// Serializer should get passed property context even if contained in an array.
public void testMethodAnnotationInArray() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test", Version.unknownVersion());
module.addSerializer(String.class, new AnnotatedContextualSerializer());
mapper.registerModule(module);
ContextualArrayBean beans = new ContextualArrayBean("123");
assertEquals("{\"beans\":[\"array->123\"]}", mapper.writeValueAsString(beans));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class TestExceptionsDuringWriting method testCatchAndRethrow.
/*
/**********************************************************
/* Tests
/**********************************************************
*/
/**
* Unit test that verifies that by default all exceptions except for
* JsonMappingException are caught and wrapped.
*/
public void testCatchAndRethrow() throws Exception {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("test-exceptions", Version.unknownVersion());
module.addSerializer(Bean.class, new SerializerWithErrors());
mapper.registerModule(module);
try {
StringWriter sw = new StringWriter();
/* And just to make things more interesting, let's create
* a nested data struct...
*/
Bean[] b = { new Bean() };
List<Bean[]> l = new ArrayList<Bean[]>();
l.add(b);
mapper.writeValue(sw, l);
fail("Should have gotten an exception");
} catch (IOException e) {
// should contain original message somewhere
verifyException(e, "test string");
Throwable root = e.getCause();
assertNotNull(root);
if (!(root instanceof IllegalArgumentException)) {
fail("Wrapped exception not IAE, but " + root.getClass());
}
}
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class EnumDeserializationTest method testGenericEnumDeserialization.
public void testGenericEnumDeserialization() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("foobar");
module.addDeserializer(Enum.class, new LcEnumDeserializer());
mapper.registerModule(module);
// not sure this is totally safe but...
assertEquals(TestEnum.JACKSON, mapper.readValue(quote("jackson"), TestEnum.class));
}
use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.
the class UntypedDeserializationTest method testNonVanilla.
// Test that exercises non-vanilla variant, with just one simple custom deserializer
public void testNonVanilla() throws IOException {
SimpleModule m = new SimpleModule("test-module");
m.addDeserializer(String.class, new UCStringDeserializer());
final ObjectMapper mapper = new ObjectMapper().registerModule(m);
// Also: since this is now non-vanilla variant, try more alternatives
List<?> l = (List<?>) mapper.readValue("[ true, false, 7, 0.5, \"foo\"]", Object.class);
assertEquals(5, l.size());
assertEquals(Boolean.TRUE, l.get(0));
assertEquals(Boolean.FALSE, l.get(1));
assertEquals(Integer.valueOf(7), l.get(2));
assertEquals(Double.valueOf(0.5), l.get(3));
assertEquals("FOO", l.get(4));
l = (List<?>) mapper.readValue("[ {}, [] ]", Object.class);
assertEquals(2, l.size());
assertTrue(l.get(0) instanceof Map<?, ?>);
assertTrue(l.get(1) instanceof List<?>);
ObjectReader rDefault = mapper.readerFor(WrappedPolymorphicUntyped.class);
ObjectReader rAlt = rDefault.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, DeserializationFeature.USE_BIG_INTEGER_FOR_INTS);
WrappedPolymorphicUntyped w;
w = rDefault.readValue(aposToQuotes("{'value':10}"));
assertEquals(Integer.valueOf(10), w.value);
w = rAlt.readValue(aposToQuotes("{'value':10}"));
assertEquals(BigInteger.TEN, w.value);
w = rDefault.readValue(aposToQuotes("{'value':5.0}"));
assertEquals(Double.valueOf(5.0), w.value);
w = rAlt.readValue(aposToQuotes("{'value':5.0}"));
assertEquals(new BigDecimal("5.0"), w.value);
StringBuilder sb = new StringBuilder(100).append("[0");
for (int i = 1; i < 100; ++i) {
sb.append(", ").append(i);
}
sb.append("]");
final String INT_ARRAY_JSON = sb.toString();
// First read as-is, no type wrapping
Object ob = mapper.readerFor(Object.class).with(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY).readValue(INT_ARRAY_JSON);
assertTrue(ob instanceof Object[]);
Object[] obs = (Object[]) ob;
for (int i = 0; i < 100; ++i) {
assertEquals(Integer.valueOf(i), obs[i]);
}
}
Aggregations