Search in sources :

Example 46 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 47 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project logging-log4j2 by apache.

the class AbstractJacksonFactory method newWriter.

public ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact, final boolean includeMillis) {
    final SimpleFilterProvider filters = new SimpleFilterProvider();
    final Set<String> except = new HashSet<>(5);
    if (!locationInfo) {
        except.add(this.getPropertyNameForSource());
    }
    if (!properties) {
        except.add(this.getPropertyNameForContextMap());
    }
    if (!includeStacktrace) {
        except.add(this.getPropertyNameForStackTrace());
    }
    if (includeMillis) {
        except.add(getPropertyNameForInstant());
    } else {
        except.add(getPropertyNameForTimeMillis());
    }
    except.add(this.getPropertyNameForNanoTime());
    filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
    final ObjectWriter writer = this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
    return writer.with(filters);
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) HashSet(java.util.HashSet)

Example 48 with SimpleFilterProvider

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

the class ConfigSupport method createMapper.

private ObjectMapper createMapper(JsonFactory mapping, ClassLoader classLoader) {
    ObjectMapper mapper = new ObjectMapper(mapping);
    mapper.addMixIn(Config.class, ConfigMixIn.class);
    mapper.addMixIn(ReferenceCodecProvider.class, ClassMixIn.class);
    mapper.addMixIn(AddressResolverGroupFactory.class, ClassMixIn.class);
    mapper.addMixIn(Codec.class, ClassMixIn.class);
    mapper.addMixIn(RedissonNodeInitializer.class, ClassMixIn.class);
    mapper.addMixIn(LoadBalancer.class, ClassMixIn.class);
    mapper.addMixIn(NatMapper.class, ClassMixIn.class);
    mapper.addMixIn(NameMapper.class, ClassMixIn.class);
    mapper.addMixIn(NettyHook.class, ClassMixIn.class);
    FilterProvider filterProvider = new SimpleFilterProvider().addFilter("classFilter", SimpleBeanPropertyFilter.filterOutAllExcept());
    mapper.setFilterProvider(filterProvider);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    if (classLoader != null) {
        TypeFactory tf = TypeFactory.defaultInstance().withClassLoader(classLoader);
        mapper.setTypeFactory(tf);
    }
    return mapper;
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 49 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method filters.

@Test
void filters() throws JsonProcessingException {
    ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().filters(new SimpleFilterProvider().setFailOnUnknownId(false)).build();
    JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
    String output = objectMapper.writeValueAsString(bean);
    assertThat(output).contains("value1");
    assertThat(output).contains("value2");
    SimpleFilterProvider provider = new SimpleFilterProvider().setFailOnUnknownId(false).setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    objectMapper = Jackson2ObjectMapperBuilder.json().filters(provider).build();
    output = objectMapper.writeValueAsString(bean);
    assertThat(output).contains("value1");
    assertThat(output).doesNotContain("value2");
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 50 with SimpleFilterProvider

use of com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider in project dhis2-core by dhis2.

the class FieldFilterService method getSimpleFilterProvider.

private SimpleFilterProvider getSimpleFilterProvider(List<FieldPath> fieldPaths, boolean skipSharing) {
    SimpleFilterProvider filterProvider = new SimpleFilterProvider();
    filterProvider.addFilter("field-filter", new FieldFilterSimpleBeanPropertyFilter(fieldPaths, skipSharing));
    return filterProvider;
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Aggregations

SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)52 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 IOException (java.io.IOException)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