Search in sources :

Example 56 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project Gaffer by gchq.

the class RoaringBitmapJsonSerialisationTest method testCanSerialiseWithCustomObjectMapper.

@Test
public void testCanSerialiseWithCustomObjectMapper() throws IOException {
    //Bitmap of (2,3000,300000) serialised in 0.5.11 Roaring Bitmap base 64 encoded
    String serialisedComparisonBitmap = "{\"roaringBitmap\":{\"value\":\"OjAAAAIAAAAAAAEABAAAABgAAAAcAAAAAgC4C+CT\"}}";
    RoaringBitmap comparisonBitmap = new RoaringBitmap();
    comparisonBitmap.add(2);
    comparisonBitmap.add(3000);
    comparisonBitmap.add(300000);
    final ObjectMapper mapper = JSONSerialiser.createDefaultMapper();
    final SimpleModule bitmapModule = new SimpleModule(RoaringBitmapConstants.BITMAP_MODULE_NAME, new Version(1, 0, 9, null, null, null));
    bitmapModule.addSerializer(RoaringBitmap.class, new RoaringBitmapJsonSerialiser());
    bitmapModule.addDeserializer(RoaringBitmap.class, new RoaringBitmapJsonDeserialiser());
    mapper.registerModule(bitmapModule);
    RoaringBitmap testBitmap = mapper.readValue(serialisedComparisonBitmap, RoaringBitmap.class);
    assertEquals(comparisonBitmap, testBitmap);
    String serialisedBitmap = mapper.writeValueAsString(testBitmap);
    assertEquals(serialisedBitmap, serialisedComparisonBitmap);
}
Also used : Version(com.fasterxml.jackson.core.Version) RoaringBitmap(org.roaringbitmap.RoaringBitmap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Test(org.junit.Test)

Example 57 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project kafka by apache.

the class VerifiableConsumer method addKafkaSerializerModule.

private void addKafkaSerializerModule() {
    SimpleModule kafka = new SimpleModule();
    kafka.addSerializer(TopicPartition.class, new JsonSerializer<TopicPartition>() {

        @Override
        public void serialize(TopicPartition tp, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartObject();
            gen.writeObjectField("topic", tp.topic());
            gen.writeObjectField("partition", tp.partition());
            gen.writeEndObject();
        }
    });
    mapper.registerModule(kafka);
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 58 with SimpleModule

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

the class TestStdSerializerOverrides method testStringSerOverideWithAfterburner.

public void testStringSerOverideWithAfterburner() throws Exception {
    final FooBean input = new FooBean();
    final String EXP = "{\"field\":\"Foo:value\"}";
    String json = mapperWithModule().registerModule(new SimpleModule("module", Version.unknownVersion()).addSerializer(String.class, new MyStringSerializer())).writeValueAsString(input);
    assertEquals(EXP, json);
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 59 with SimpleModule

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

the class TestStdDeserializerOverrides method testStringDeserOverideWithAfterburner.

// for [module-afterburner#59]
public void testStringDeserOverideWithAfterburner() throws Exception {
    final String json = "{\"field\": \"value &amp; value\"}";
    final String EXP = "value & value";
    final Module module = new SimpleModule("module", Version.unknownVersion()) {

        @Override
        public void setupModule(SetupContext context) {
            context.addDeserializers(new Deserializers.Base() {

                @Override
                public JsonDeserializer<?> findBeanDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException {
                    if (type.hasRawClass(String.class)) {
                        return new DeAmpDeserializer();
                    }
                    return null;
                }
            });
        }
    };
    // but then fails with Afterburner
    Issue59Bean resultAB = mapperWithModule().registerModule(module).readValue(json, Issue59Bean.class);
    assertEquals(EXP, resultAB.field);
}
Also used : Deserializers(com.fasterxml.jackson.databind.deser.Deserializers) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 60 with SimpleModule

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

the class CustomBeanPropertyWriterTest method testCustomPropertyWriter.

public void testCustomPropertyWriter() throws Exception {
    ObjectMapper objectMapper = mapperWithModule();
    SimpleModule simpleModule = new SimpleModule();
    simpleModule.setSerializerModifier(new Only2BeanSerializerModifier());
    objectMapper.registerModule(simpleModule);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    SampleObject sampleObject = new SampleObject(null, 2, 3);
    String json = objectMapper.writeValueAsString(sampleObject);
    assertEquals("{\"field2\":2}", json);
}
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