Search in sources :

Example 66 with SimpleModule

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

Example 67 with SimpleModule

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

Example 68 with SimpleModule

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

Example 69 with SimpleModule

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

Example 70 with SimpleModule

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")));
}
Also used : 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