Search in sources :

Example 6 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project credhub by cloudfoundry-incubator.

the class TimeModuleFactory method createTimeModule.

public static JavaTimeModule createTimeModule() {
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(Instant.class, new JsonSerializer<Instant>() {

        @Override
        public void serialize(Instant value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeString(ZonedDateTime.ofInstant(value, ZoneId.of("UTC")).format(TIMESTAMP_FORMAT));
        }
    });
    return javaTimeModule;
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Instant(java.time.Instant) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 7 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project rpki-validator-3 by RIPE-NCC.

the class ApiConfig method customizeLinksRendering.

@Bean
public Jackson2ObjectMapperBuilderCustomizer customizeLinksRendering() {
    return (jacksonObjectMapperBuilder) -> {
        jacksonObjectMapperBuilder.serializerByType(Links.class, new JsonObjectSerializer<Links>() {

            @Override
            protected void serializeObject(Links value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
                for (Link link : value) {
                    jgen.writeStringField(link.getRel(), link.getHref());
                }
            }
        });
        jacksonObjectMapperBuilder.deserializerByType(Links.class, new JsonObjectDeserializer<Links>() {

            @Override
            protected Links deserializeObject(JsonParser jsonParser, DeserializationContext context, ObjectCodec codec, JsonNode tree) throws IOException {
                Iterator<Map.Entry<String, JsonNode>> iterator = tree.fields();
                List<Link> links = new ArrayList<>();
                while (iterator.hasNext()) {
                    Map.Entry<String, JsonNode> field = iterator.next();
                    links.add(new Link(field.getValue().asText(), field.getKey()));
                }
                return new Links(links);
            }
        });
    };
}
Also used : Jackson2ObjectMapperBuilderCustomizer(org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer) Link(org.springframework.hateoas.Link) java.util(java.util) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Links(org.springframework.hateoas.Links) MediaType(org.springframework.http.MediaType) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) IOException(java.io.IOException) Configuration(org.springframework.context.annotation.Configuration) Api(net.ripe.rpki.rtr.api.Api) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) JsonObjectDeserializer(org.springframework.boot.jackson.JsonObjectDeserializer) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ContentNegotiationConfigurer(org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Bean(org.springframework.context.annotation.Bean) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) JsonObjectDeserializer(org.springframework.boot.jackson.JsonObjectDeserializer) Links(org.springframework.hateoas.Links) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Link(org.springframework.hateoas.Link) JsonParser(com.fasterxml.jackson.core.JsonParser) Bean(org.springframework.context.annotation.Bean)

Example 8 with SerializerProvider

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

the class ServletResponseResultWriter method getWriteDateAndTimeAsStringModule.

private static SimpleModule getWriteDateAndTimeAsStringModule() {
    JsonSerializer<DateAndTime> dateAndTimeSerializer = new JsonSerializer<DateAndTime>() {

        @Override
        public void serialize(DateAndTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.toRfc3339String());
        }
    };
    SimpleModule writeDateAsStringModule = new SimpleModule("writeDateAsStringModule", new Version(1, 0, 0, null, null, null));
    writeDateAsStringModule.addSerializer(DateAndTime.class, dateAndTimeSerializer);
    return writeDateAsStringModule;
}
Also used : Version(com.fasterxml.jackson.core.Version) DateAndTime(com.google.api.server.spi.types.DateAndTime) 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 9 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project druid by druid-io.

the class GroupByQueryQueryToolChest method decorateObjectMapper.

@Override
public ObjectMapper decorateObjectMapper(final ObjectMapper objectMapper, final GroupByQuery query) {
    final boolean resultAsArray = query.getContextBoolean(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false);
    // Serializer that writes array- or map-based rows as appropriate, based on the "resultAsArray" setting.
    final JsonSerializer<ResultRow> serializer = new JsonSerializer<ResultRow>() {

        @Override
        public void serialize(final ResultRow resultRow, final JsonGenerator jg, final SerializerProvider serializers) throws IOException {
            if (resultAsArray) {
                jg.writeObject(resultRow.getArray());
            } else {
                jg.writeObject(resultRow.toMapBasedRow(query));
            }
        }
    };
    // Deserializer that can deserialize either array- or map-based rows.
    final JsonDeserializer<ResultRow> deserializer = new JsonDeserializer<ResultRow>() {

        @Override
        public ResultRow deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
            if (jp.isExpectedStartObjectToken()) {
                final Row row = jp.readValueAs(Row.class);
                return ResultRow.fromLegacyRow(row, query);
            } else {
                return ResultRow.of(jp.readValueAs(Object[].class));
            }
        }
    };
    class GroupByResultRowModule extends SimpleModule {

        private GroupByResultRowModule() {
            addSerializer(ResultRow.class, serializer);
            addDeserializer(ResultRow.class, deserializer);
        }
    }
    final ObjectMapper newObjectMapper = objectMapper.copy();
    newObjectMapper.registerModule(new GroupByResultRowModule());
    return newObjectMapper;
}
Also used : JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) Row(org.apache.druid.data.input.Row) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class PageSerializerTest method serialize.

@Test
public void serialize() throws Exception {
    Page page = mock(Page.class);
    when(page.getName()).thenReturn(PAGE_NAME);
    when(page.getTitle()).thenReturn(PAGE_TITLE);
    when(page.getPageTitle()).thenReturn(PAGE_TITLE);
    when(page.getPath()).thenReturn(PAGE_PATH);
    when(page.getDescription()).thenReturn(PAGE_DESCRIPTION);
    JsonGenerator jsonGenerator = mock(JsonGenerator.class);
    SerializerProvider serializerProvider = mock(SerializerProvider.class);
    PageSerializer pageSerializer = new PageSerializer(Page.class);
    pageSerializer.serialize(page, jsonGenerator, serializerProvider);
    verify(jsonGenerator).writeStartObject();
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_NAME, page.getName());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_TITLE, page.getTitle());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PAGE_TITLE, page.getPageTitle());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_PATH, page.getPath());
    verify(jsonGenerator).writeStringField(PageSerializer.JSON_KEY_DESCRIPTION, page.getDescription());
    verify(jsonGenerator).writeEndObject();
    verifyNoMoreInteractions(jsonGenerator);
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Page(com.day.cq.wcm.api.Page) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) 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