Search in sources :

Example 86 with SimpleModule

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

the class SimpleModuleTest method testSimpleInterfaceSerializer.

public void testSimpleInterfaceSerializer() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    mod.addSerializer(new BaseSerializer());
    // and another variant here too
    List<Module> mods = Arrays.asList((Module) mod);
    mapper.registerModules(mods);
    assertEquals(quote("Base:1"), mapper.writeValueAsString(new Impl1()));
    assertEquals(quote("Base:2"), mapper.writeValueAsString(new Impl2()));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 87 with SimpleModule

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

the class SimpleModuleTest method testSimpleBeanDeserializer.

/*
    /**********************************************************
    /* Unit tests; simple deserializers
    /**********************************************************
     */
public void testSimpleBeanDeserializer() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    mod.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
    mapper.registerModule(mod);
    CustomBean bean = mapper.readValue(quote("xyz|3"), CustomBean.class);
    assertEquals("xyz", bean.str);
    assertEquals(3, bean.num);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 88 with SimpleModule

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

the class SimpleModuleTest method testMultipleModules.

public void testMultipleModules() throws Exception {
    MySimpleModule mod1 = new MySimpleModule("test1", Version.unknownVersion());
    SimpleModule mod2 = new SimpleModule("test2", Version.unknownVersion());
    mod1.addSerializer(SimpleEnum.class, new SimpleEnumSerializer());
    mod1.addDeserializer(CustomBean.class, new CustomBeanDeserializer());
    Map<Class<?>, JsonDeserializer<?>> desers = new HashMap<>();
    desers.put(SimpleEnum.class, new SimpleEnumDeserializer());
    mod2.setDeserializers(new SimpleDeserializers(desers));
    mod2.addSerializer(CustomBean.class, new CustomBeanSerializer());
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(mod1);
    mapper.registerModule(mod2);
    assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
    SimpleEnum result = mapper.readValue(quote("a"), SimpleEnum.class);
    assertSame(SimpleEnum.A, result);
    // also let's try it with different order of registration, just in case
    mapper = new ObjectMapper();
    mapper.registerModule(mod2);
    mapper.registerModule(mod1);
    assertEquals(quote("b"), mapper.writeValueAsString(SimpleEnum.B));
    result = mapper.readValue(quote("a"), SimpleEnum.class);
    assertSame(SimpleEnum.A, result);
}
Also used : SimpleDeserializers(com.fasterxml.jackson.databind.module.SimpleDeserializers) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 89 with SimpleModule

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

the class TestAbstractTypes method testDefaultingRecursive.

// [databind#700]
public void testDefaultingRecursive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    // defaults: LinkedHashMap, ArrayList
    mod.addAbstractTypeMapping(Map.class, TreeMap.class);
    mod.addAbstractTypeMapping(List.class, LinkedList.class);
    mapper.registerModule(mod);
    Object result;
    result = mapper.readValue("[ {} ]", Object.class);
    assertEquals(LinkedList.class, result.getClass());
    Object v = ((List<?>) result).get(0);
    assertNotNull(v);
    assertEquals(TreeMap.class, v.getClass());
    result = mapper.readValue("{ \"x\": [ 3 ] }", Object.class);
    assertEquals(TreeMap.class, result.getClass());
    Map<?, ?> map = (Map<?, ?>) result;
    assertEquals(1, map.size());
    v = map.get("x");
    assertNotNull(v);
    assertEquals(LinkedList.class, v.getClass());
    assertEquals(1, ((List<?>) v).size());
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 90 with SimpleModule

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

the class TestAbstractTypes method testInterfaceDefaulting.

public void testInterfaceDefaulting() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
    // let's ensure we get hierarchic mapping
    mod.addAbstractTypeMapping(CharSequence.class, MyString.class);
    mapper.registerModule(mod);
    Object result = mapper.readValue(quote("abc"), CharSequence.class);
    assertEquals(MyString.class, result.getClass());
    assertEquals("abc", ((MyString) result).value);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)112 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)41 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 IOException (java.io.IOException)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)3 Module (com.fasterxml.jackson.databind.Module)3 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)3