Search in sources :

Example 21 with SimpleFilterProvider

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

the class Jackson2ObjectMapperFactoryBeanTests method setFilters.

@Test
public void setFilters() throws JsonProcessingException {
    this.factory.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
    this.factory.afterPropertiesSet();
    ObjectMapper objectMapper = this.factory.getObject();
    JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
    String output = objectMapper.writeValueAsString(bean);
    assertThat(output).contains("value1");
    assertThat(output).contains("value2");
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 22 with SimpleFilterProvider

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

the class MappingJackson2HttpMessageConverterTests method filters.

@Test
public void filters() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    JacksonFilteredBean bean = new JacksonFilteredBean();
    bean.setProperty1("value");
    bean.setProperty2("value");
    MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
    FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    jacksonValue.setFilters(filters);
    this.converter.writeInternal(jacksonValue, null, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertThat(result).contains("\"property1\":\"value\"");
    assertThat(result).doesNotContain("\"property2\":\"value\"");
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.jupiter.api.Test)

Example 23 with SimpleFilterProvider

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

the class MappingJackson2JsonViewTests method renderSimpleBeanWithFilters.

@Test
public void renderSimpleBeanWithFilters() throws Exception {
    TestSimpleBeanFiltered bean = new TestSimpleBeanFiltered();
    bean.setProperty1("value");
    bean.setProperty2("value");
    Map<String, Object> model = new HashMap<>();
    model.put("bindingResult", mock(BindingResult.class, "binding_result"));
    model.put("foo", bean);
    FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    model.put(FilterProvider.class.getName(), filters);
    view.setUpdateContentLength(true);
    view.render(model, request, response);
    String content = response.getContentAsString();
    assertThat(content.length() > 0).isTrue();
    assertThat(response.getContentLength()).isEqualTo(content.length());
    assertThat(content).contains("\"property1\":\"value\"");
    assertThat(content).doesNotContain("\"property2\":\"value\"");
    assertThat(content.contains(FilterProvider.class.getName())).isFalse();
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) BindingResult(org.springframework.validation.BindingResult) HashMap(java.util.HashMap) ScriptableObject(org.mozilla.javascript.ScriptableObject) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.jupiter.api.Test)

Example 24 with SimpleFilterProvider

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

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

the class CasCoreConfigurationUtilsTests method verifyMapping.

@Test
public void verifyMapping() {
    val props = new CasConfigurationProperties();
    props.getAuthn().getSyncope().setName("SyncopeAuth");
    props.getAuthn().getSyncope().setUrl("https://github.com/apereo/cas");
    props.getAuthn().getSyncope().setDomain("Master");
    val filters = new SimpleFilterProvider().setFailOnUnknownId(false).addFilter(CasConfigurationProperties.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept("authn")).addFilter(AuthenticationProperties.class.getSimpleName(), SimpleBeanPropertyFilter.filterOutAllExcept("syncope"));
    val map = CasCoreConfigurationUtils.asMap(props.withHolder(), filters);
    assertTrue(map.keySet().stream().allMatch(key -> key.startsWith("cas.authn.syncope")));
}
Also used : lombok.val(lombok.val) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) RefreshAutoConfiguration(org.springframework.cloud.autoconfigure.RefreshAutoConfiguration) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) lombok.val(lombok.val) Assertions(org.junit.jupiter.api.Assertions) Autowired(org.springframework.beans.factory.annotation.Autowired) AuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AuthenticationProperties) SimpleBeanPropertyFilter(com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter) Tag(org.junit.jupiter.api.Tag) LdapAuthenticationProperties(org.apereo.cas.configuration.model.support.ldap.LdapAuthenticationProperties) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) AuthenticationProperties(org.apereo.cas.configuration.model.core.authentication.AuthenticationProperties) LdapAuthenticationProperties(org.apereo.cas.configuration.model.support.ldap.LdapAuthenticationProperties) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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