Search in sources :

Example 11 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project tutorials by eugenp.

the class JacksonSerializationIgnoreUnitTest method givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect.

@Test
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
    final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
    final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
    dtoObject.setIntValue(12);
    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 : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) Matchers.containsString(org.hamcrest.Matchers.containsString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MyDtoWithFilter(com.baeldung.jackson.dtos.MyDtoWithFilter) Test(org.junit.Test)

Example 12 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider 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)

Example 13 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project tutorials by eugenp.

the class JacksonAnnotationUnitTest method whenSerializingUsingJsonFilter_thenCorrect.

@Test
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
    final BeanWithFilter bean = new BeanWithFilter(1, "My bean");
    final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
    final String result = new ObjectMapper().writer(filters).writeValueAsString(bean);
    assertThat(result, containsString("My bean"));
    assertThat(result, not(containsString("id")));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) BeanWithFilter(com.baeldung.jackson.annotation.BeanWithFilter) Matchers.containsString(org.hamcrest.Matchers.containsString) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project tutorials by eugenp.

the class JsonFilterUnitTest method whenSerializingUsingJsonFilter_thenCorrect.

@Test
public void whenSerializingUsingJsonFilter_thenCorrect() throws JsonProcessingException {
    // arrange
    Author author = new Author("Alex", "Theedom");
    FilterProvider filters = new SimpleFilterProvider().addFilter("authorFilter", SimpleBeanPropertyFilter.filterOutAllExcept("lastName"));
    // act
    String result = new ObjectMapper().writer(filters).writeValueAsString(author);
    // assert
    assertThat(from(result).getList("items")).isNull();
/*
            {
              "lastName": "Theedom"
            }
        */
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project candlepin by candlepin.

the class JsonProvider method configureHateoasObjectMapper.

private void configureHateoasObjectMapper(ObjectMapper mapper, boolean indentJson) {
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    if (indentJson) {
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    }
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider = filterProvider.addFilter("ConsumerFilter", new MultiFilter(new HateoasBeanPropertyFilter(), new DynamicPropertyFilter()));
    filterProvider = filterProvider.addFilter("EntitlementFilter", new MultiFilter(new HateoasBeanPropertyFilter(), new DynamicPropertyFilter()));
    filterProvider = filterProvider.addFilter("OwnerFilter", new MultiFilter(new HateoasBeanPropertyFilter(), new DynamicPropertyFilter()));
    filterProvider = filterProvider.addFilter("GuestFilter", new MultiFilter(new HateoasBeanPropertyFilter(), new DynamicPropertyFilter()));
    filterProvider.setDefaultFilter(new DynamicPropertyFilter());
    filterProvider.setFailOnUnknownId(false);
    mapper.setFilterProvider(filterProvider);
    AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
    AnnotationIntrospector pair = new AnnotationIntrospectorPair(primary, secondary);
    mapper.setAnnotationIntrospector(pair);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospectorPair(com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) HateoasBeanPropertyFilter(org.candlepin.common.jackson.HateoasBeanPropertyFilter) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector) DynamicPropertyFilter(org.candlepin.common.jackson.DynamicPropertyFilter) MultiFilter(org.candlepin.common.jackson.MultiFilter) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector)

Aggregations

SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)51 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)21 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)17 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)12 SimpleBeanPropertyFilter (com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter)11 Test (org.junit.Test)10 ApiPermissionFilter (io.gravitee.management.service.jackson.filter.ApiPermissionFilter)9 Before (org.junit.Before)9 Test (org.junit.jupiter.api.Test)7 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 HashSet (java.util.HashSet)4 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)3 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)3 lombok.val (lombok.val)3 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 AuthenticationProperties (org.apereo.cas.configuration.model.core.authentication.AuthenticationProperties)3 LdapAuthenticationProperties (org.apereo.cas.configuration.model.support.ldap.LdapAuthenticationProperties)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3