custom jackson serialization
This commit is contained in:
parent
ca5d33bb9a
commit
78abeaf838
|
@ -15,9 +15,14 @@ import org.baeldung.jackson.ignore.MyDtoWithFilter;
|
|||
import org.baeldung.jackson.ignore.MyMixInForString;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.FilterProvider;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.PropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
|
||||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
|
||||
import com.google.common.collect.Lists;
|
||||
|
@ -88,7 +93,7 @@ public class JacksonSerializationUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresIntsOver10_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||
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);
|
||||
|
@ -104,6 +109,49 @@ public class JacksonSerializationUnitTest {
|
|||
System.out.println(dtoAsString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenTypeHasFilterThatIgnoresIntsOver10_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")) {
|
||||
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
|
||||
if (intValue < 10) {
|
||||
writer.serializeAsField(pojo, jgen, provider);
|
||||
}
|
||||
} else {
|
||||
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(12);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// tests - multiple entities to json
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue