Search in sources :

Example 1 with SimpleBeanPropertyFilter

use of com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter 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 2 with SimpleBeanPropertyFilter

use of com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter 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 3 with SimpleBeanPropertyFilter

use of com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter in project eureka by Netflix.

the class AbstractEurekaJacksonCodec method bindAmazonInfoFilter.

private void bindAmazonInfoFilter(ObjectMapper mapper) {
    SimpleFilterProvider filters = new SimpleFilterProvider();
    final String filterName = "exclude-amazon-info-entries";
    mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {

        @Override
        public Object findFilterId(Annotated a) {
            if (Map.class.isAssignableFrom(a.getRawType())) {
                return filterName;
            }
            return super.findFilterId(a);
        }
    });
    filters.addFilter(filterName, new SimpleBeanPropertyFilter() {

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

        @Override
        protected boolean include(PropertyWriter writer) {
            return MINI_AMAZON_INFO_INCLUDE_KEYS.contains(writer.getName());
        }
    });
    mapper.setFilters(filters);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) Map(java.util.Map) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) PropertyWriter(com.fasterxml.jackson.databind.ser.PropertyWriter)

Example 4 with SimpleBeanPropertyFilter

use of com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter in project beam by apache.

the class MetricsHttpSink method serializeMetrics.

private String serializeMetrics(MetricQueryResults metricQueryResults) throws Exception {
    SimpleModule module = new JodaModule();
    module.addSerializer(new MetricNameSerializer(MetricName.class));
    module.addSerializer(new MetricKeySerializer(MetricKey.class));
    module.addSerializer(new MetricResultSerializer(MetricResult.class));
    objectMapper.registerModule(module);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    // need to register a filter as soon as @JsonFilter annotation is specified.
    // So specify an pass through filter
    SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAll();
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider.addFilter("committedMetrics", filter);
    objectMapper.setFilterProvider(filterProvider);
    String result;
    try {
        result = objectMapper.writeValueAsString(metricQueryResults);
    } catch (JsonMappingException exception) {
        if ((exception.getCause() instanceof UnsupportedOperationException) && exception.getCause().getMessage().contains("committed metrics")) {
            filterProvider.removeFilter("committedMetrics");
            filter = SimpleBeanPropertyFilter.serializeAllExcept("committed");
            filterProvider.addFilter("committedMetrics", filter);
            result = objectMapper.writeValueAsString(metricQueryResults);
        } else {
            throw exception;
        }
    }
    return result;
}
Also used : JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) MetricResult(org.apache.beam.sdk.metrics.MetricResult) MetricName(org.apache.beam.sdk.metrics.MetricName) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MetricKey(org.apache.beam.sdk.metrics.MetricKey) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 5 with SimpleBeanPropertyFilter

use of com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter in project instagram-java-scraper by postaddictme.

the class MappingTest method toJson.

private String toJson(Object instance) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.addMixIn(Object.class, PropertyFilterMixIn.class);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("lastUpdated");
    FilterProvider filters = new SimpleFilterProvider().addFilter("skipLastUpdated", theFilter);
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    return objectMapper.writer(filters).writeValueAsString(instance);
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Aggregations

SimpleBeanPropertyFilter (com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter)5 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)3 MyDtoWithFilter (com.baeldung.jackson.dtos.MyDtoWithFilter)2 BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)2 PropertyWriter (com.fasterxml.jackson.databind.ser.PropertyWriter)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 PropertyFilter (com.fasterxml.jackson.databind.ser.PropertyFilter)1 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)1 Map (java.util.Map)1 MetricKey (org.apache.beam.sdk.metrics.MetricKey)1 MetricName (org.apache.beam.sdk.metrics.MetricName)1