Search in sources :

Example 1 with JsonObjectSerializer

use of org.springframework.boot.jackson.JsonObjectSerializer 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 2 with JsonObjectSerializer

use of org.springframework.boot.jackson.JsonObjectSerializer 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) Arrays(java.util.Arrays) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Links(org.springframework.hateoas.Links) Autowired(org.springframework.beans.factory.annotation.Autowired) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) ApiLogger(net.ripe.rpki.validator3.api.ApiLogger) ArrayList(java.util.ArrayList) Api(net.ripe.rpki.validator3.api.Api) InterceptorRegistry(org.springframework.web.servlet.config.annotation.InterceptorRegistry) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Link(org.springframework.hateoas.Link) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) Iterator(java.util.Iterator) MediaType(org.springframework.http.MediaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) 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) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ContentNegotiationConfigurer(org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer) Bean(org.springframework.context.annotation.Bean) WebMvcConfigurer(org.springframework.web.servlet.config.annotation.WebMvcConfigurer) JsonObjectSerializer(org.springframework.boot.jackson.JsonObjectSerializer) ArrayList(java.util.ArrayList) 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) Map(java.util.Map) Link(org.springframework.hateoas.Link) JsonParser(com.fasterxml.jackson.core.JsonParser) Bean(org.springframework.context.annotation.Bean)

Aggregations

JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)2 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)2 IOException (java.io.IOException)2 Jackson2ObjectMapperBuilderCustomizer (org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer)2 JsonObjectDeserializer (org.springframework.boot.jackson.JsonObjectDeserializer)2 JsonObjectSerializer (org.springframework.boot.jackson.JsonObjectSerializer)2 Bean (org.springframework.context.annotation.Bean)2 Configuration (org.springframework.context.annotation.Configuration)2 Link (org.springframework.hateoas.Link)2 Links (org.springframework.hateoas.Links)2 MediaType (org.springframework.http.MediaType)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)2 ContentNegotiationConfigurer (org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer)2 WebMvcConfigurer (org.springframework.web.servlet.config.annotation.WebMvcConfigurer)2