Search in sources :

Example 81 with SimpleModule

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));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 82 with SimpleModule

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]);
    }
}
Also used : BigDecimal(java.math.BigDecimal) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 83 with SimpleModule

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

the class UntypedDeserializationTest method testUntypedWithListDeser.

public void testUntypedWithListDeser() throws IOException {
    SimpleModule m = new SimpleModule("test-module");
    m.addDeserializer(List.class, new ListDeserializer());
    final ObjectMapper mapper = new ObjectMapper().registerModule(m);
    // And then list...
    Object ob = mapper.readValue("[1, 2, true]", Object.class);
    assertTrue(ob instanceof List<?>);
    List<?> l = (List<?>) ob;
    assertEquals(3, l.size());
    assertEquals("X1", l.get(0));
    assertEquals("X2", l.get(1));
    assertEquals("Xtrue", l.get(2));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 84 with SimpleModule

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

the class TestKeySerializers method testUnWrappedMapWithDefaultType.

// [databind#838]
public void testUnWrappedMapWithDefaultType() throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test");
    mod.addKeySerializer(ABC.class, new ABCKeySerializer());
    mapper.registerModule(mod);
    TypeResolverBuilder<?> typer = new ObjectMapper.DefaultTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL);
    typer = typer.init(JsonTypeInfo.Id.NAME, null);
    typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
    //typer = typer.typeProperty(TYPE_FIELD);
    typer = typer.typeIdVisibility(true);
    mapper.setDefaultTyping(typer);
    Map<ABC, String> stuff = new HashMap<ABC, String>();
    stuff.put(ABC.B, "bar");
    String json = mapper.writerFor(new TypeReference<Map<ABC, String>>() {
    }).writeValueAsString(stuff);
    assertEquals("{\"@type\":\"HashMap\",\"xxxB\":\"bar\"}", json);
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 85 with SimpleModule

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

the class TestKeySerializers method testCustomForEnum.

// Test custom key serializer for enum
public void testCustomForEnum() throws IOException {
    // can not use shared mapper as we are registering a module
    final ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test");
    mod.addKeySerializer(ABC.class, new ABCKeySerializer());
    mapper.registerModule(mod);
    String json = mapper.writeValueAsString(new ABCMapWrapper());
    assertEquals("{\"stuff\":{\"xxxB\":\"bar\"}}", json);
}
Also used : 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