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");
}
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\"");
}
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();
}
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;
}
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")));
}
Aggregations