Search in sources :

Example 1 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.Test)

Example 2 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.

the class DescriptionFactoryWrapper method expectArrayFormat.

@Override
public JsonArrayFormatVisitor expectArrayFormat(JavaType convertedType) {
    final JsonPointer jsonPointer = getJsonPointer();
    return new JsonArrayFormatVisitor.Base(getProvider()) {

        @Override
        public void itemsFormat(JsonFormatVisitable handler, JavaType elementType) throws JsonMappingException {
            SerializerProvider p = getProvider();
            JsonSerializer<Object> s = p.findValueSerializer(elementType);
            s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(DescriptionFactoryWrapper.this, jsonPointer.append(JsonPointer.valueOf("/<index>")), p), elementType);
        }
    };
}
Also used : JsonFormatVisitable(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable) JavaType(com.fasterxml.jackson.databind.JavaType) JsonPointer(com.fasterxml.jackson.core.JsonPointer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 3 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.

the class DescriptionFactoryWrapper method expectObjectFormat.

@Override
public JsonObjectFormatVisitor expectObjectFormat(JavaType convertedType) {
    final JsonPointer jsonPointer = getJsonPointer();
    return new JsonObjectFormatVisitor.Base(getProvider()) {

        @Override
        public void property(BeanProperty prop) throws JsonMappingException {
            visitProperty(prop, false);
        }

        @Override
        public void optionalProperty(BeanProperty prop) throws JsonMappingException {
            visitProperty(prop, true);
        }

        private void visitProperty(BeanProperty prop, boolean optional) throws JsonMappingException {
            JsonPointer propPointer = jsonPointer.append(JsonPointer.valueOf("/" + prop.getName()));
            document(propPointer, prop);
            SerializerProvider p = getProvider();
            JsonSerializer<Object> s = p.findValueSerializer(prop.getType(), prop);
            s.acceptJsonFormatVisitor(new DescriptionFactoryWrapper(DescriptionFactoryWrapper.this, propPointer, p), prop.getType());
        }
    };
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Example 4 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project torodb by torodb.

the class AbstractBackendSerializer method acceptJsonFormatVisitor.

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
    if (visitor == null) {
        return;
    }
    JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
    SerializerProvider prov = visitor.getProvider();
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(type);
    JsonSubTypes jsonSubTypes;
    if (v != null) {
        for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
            if (propDef.isExplicitlyIncluded()) {
                jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
                if (jsonSubTypes != null) {
                    for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
                        JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
                        depositSchemaProperty(v, jsonSubType.name(), subType);
                    }
                } else {
                    depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
                }
            }
        }
    }
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JavaType(com.fasterxml.jackson.databind.JavaType) JsonObjectFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 5 with SerializerProvider

use of com.fasterxml.jackson.databind.SerializerProvider in project tutorials by eugenp.

the class JacksonSerializationIgnoreUnitTest method givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect.

@Test
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
    final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {

        @Override
        public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
            if (include(writer)) {
                if (!writer.getName().equals("intValue")) {
                    writer.serializeAsField(pojo, jgen, provider);
                    return;
                }
                final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
                if (intValue >= 0) {
                    writer.serializeAsField(pojo, jgen, provider);
                }
            } else if (!jgen.canOmitFields()) {
                // since 2.3
                writer.serializeAsOmittedField(pojo, jgen, provider);
            }
        }

        @Override
        protected final boolean include(final BeanPropertyWriter writer) {
            return true;
        }

        @Override
        protected final boolean include(final PropertyWriter writer) {
            return true;
        }
    };
    final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
    final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
    dtoObject.setIntValue(-1);
    final ObjectMapper mapper = new ObjectMapper();
    final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
    assertThat(dtoAsString, not(containsString("intValue")));
    assertThat(dtoAsString, containsString("booleanValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    System.out.println(dtoAsString);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) PropertyFilter(com.fasterxml.jackson.databind.ser.PropertyFilter) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) PropertyWriter(com.fasterxml.jackson.databind.ser.PropertyWriter) MyDtoWithFilter(com.baeldung.jackson.dtos.MyDtoWithFilter) Test(org.junit.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