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);
}
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);
}
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")));
}
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"
}
*/
}
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);
}
Aggregations