Search in sources :

Example 76 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 77 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)

Example 78 with SimpleModule

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

Example 79 with SimpleModule

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

Example 80 with SimpleModule

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());
        }
    }
}
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