Search in sources :

Example 21 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriter method getWriteLongAsStringModule.

private static SimpleModule getWriteLongAsStringModule() {
    JsonSerializer<Long> longSerializer = new JsonSerializer<Long>() {

        @Override
        public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.toString());
        }
    };
    SimpleModule writeLongAsStringModule = new SimpleModule("writeLongAsStringModule", new Version(1, 0, 0, null, null, null));
    // long (primitive)
    writeLongAsStringModule.addSerializer(Long.TYPE, longSerializer);
    // Long (class)
    writeLongAsStringModule.addSerializer(Long.class, longSerializer);
    return writeLongAsStringModule;
}
Also used : Version(com.fasterxml.jackson.core.Version) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 22 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project apache-kafka-on-k8s by banzaicloud.

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 23 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project GMM by Katharsas.

the class ApplicationConfiguration method customJackson2HttpMessageConverter.

/**
 * Configure Jackson (enable Path to Json/String conversion)
 */
@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter converter) {
    final ObjectMapper objectMapper = new ObjectMapper();
    final SimpleModule sm = new SimpleModule();
    sm.addSerializer(Path.class, new StdSerializer<Path>(Path.class) {

        private static final long serialVersionUID = 8963132152002562810L;

        @Override
        public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
            gen.writeString(value.toString());
        }
    });
    objectMapper.registerModule(sm);
    converter.setObjectMapper(objectMapper);
    return converter;
}
Also used : Path(java.nio.file.Path) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 24 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider 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 25 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project dhis2-core by dhis2.

the class CustomLastUpdatedUserSerializerTest method serializeJson.

@Test
void serializeJson() throws Exception {
    Writer jsonWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonWriter);
    SerializerProvider serializerProvider = new ObjectMapper().getSerializerProvider();
    new CustomLastUpdatedUserSerializer().serialize(user, jsonGenerator, serializerProvider);
    jsonGenerator.flush();
    assertEquals("{\"id\":\"jshfdkd323\",\"name\":\"Peter Brown\"}", jsonWriter.toString());
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)26 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)20 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)11 IOException (java.io.IOException)9 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Version (com.fasterxml.jackson.core.Version)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)4 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)3 Test (org.junit.Test)3 Page (com.day.cq.wcm.api.Page)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)2 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 JavaType (com.fasterxml.jackson.databind.JavaType)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SimpleDate (com.google.api.server.spi.types.SimpleDate)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Test (org.junit.jupiter.api.Test)2