mapping work
This commit is contained in:
parent
8118dd3c65
commit
aa0030d546
|
@ -2,9 +2,9 @@ package dtos;
|
||||||
|
|
||||||
public class MyDtoNoAccessors {
|
public class MyDtoNoAccessors {
|
||||||
|
|
||||||
private String stringValue;
|
String stringValue;
|
||||||
private int intValue;
|
int intValue;
|
||||||
private boolean booleanValue;
|
boolean booleanValue;
|
||||||
|
|
||||||
public MyDtoNoAccessors() {
|
public MyDtoNoAccessors() {
|
||||||
super();
|
super();
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package dtos;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
|
|
||||||
|
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
||||||
|
public class MyDtoNoAccessorsAndFieldVisibility {
|
||||||
|
|
||||||
|
String stringValue;
|
||||||
|
int intValue;
|
||||||
|
boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoNoAccessorsAndFieldVisibility() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDtoNoAccessorsAndFieldVisibility(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,187 @@
|
||||||
|
package org.baeldung.jackson.test;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
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 dtos.MyDto;
|
||||||
|
import dtos.MyDtoIgnoreField;
|
||||||
|
import dtos.MyDtoIgnoreFieldByName;
|
||||||
|
import dtos.MyDtoIgnoreNull;
|
||||||
|
import dtos.MyDtoIncludeNonDefault;
|
||||||
|
import dtos.MyDtoWithFilter;
|
||||||
|
import dtos.MyMixInForString;
|
||||||
|
|
||||||
|
public class JacksonSerializationIgnoreUnitTest {
|
||||||
|
|
||||||
|
// tests - single entity to json
|
||||||
|
|
||||||
|
// ignore
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault());
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("intValue")));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
|
||||||
|
dtoObject.setBooleanValue(true);
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
|
||||||
|
dtoObject.setBooleanValue(true);
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("intValue")));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField();
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("intValue")));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.addMixInAnnotations(String.class, MyMixInForString.class);
|
||||||
|
final MyDto dtoObject = new MyDto();
|
||||||
|
dtoObject.setBooleanValue(true);
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.setSerializationInclusion(Include.NON_NULL);
|
||||||
|
final MyDto dtoObject = new MyDto();
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package org.baeldung.jackson.test;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -9,52 +10,30 @@ import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import dtos.MyDto;
|
import dtos.MyDto;
|
||||||
import dtos.MyDtoFieldNameChanged;
|
import dtos.MyDtoFieldNameChanged;
|
||||||
import dtos.MyDtoIgnoreField;
|
import dtos.MyDtoNoAccessors;
|
||||||
import dtos.MyDtoIgnoreFieldByName;
|
import dtos.MyDtoNoAccessorsAndFieldVisibility;
|
||||||
import dtos.MyDtoIgnoreNull;
|
|
||||||
import dtos.MyDtoIncludeNonDefault;
|
|
||||||
import dtos.MyDtoWithFilter;
|
|
||||||
import dtos.MyMixInForString;
|
|
||||||
|
|
||||||
public class JacksonSerializationUnitTest {
|
public class JacksonSerializationUnitTest {
|
||||||
|
|
||||||
// tests - single entity to json
|
// tests - single entity to json
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
public final void whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault());
|
final String dtoAsString = mapper.writeValueAsString(new MyDto());
|
||||||
|
|
||||||
assertThat(dtoAsString, not(containsString("intValue")));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
|
|
||||||
dtoObject.setBooleanValue(true);
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -70,134 +49,32 @@ public class JacksonSerializationUnitTest {
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore
|
@Test(expected = JsonMappingException.class)
|
||||||
|
public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException {
|
||||||
|
final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());
|
||||||
|
|
||||||
@Test
|
assertThat(dtoAsString, notNullValue());
|
||||||
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
final MyDtoIgnoreFieldByName dtoObject = new MyDtoIgnoreFieldByName();
|
|
||||||
dtoObject.setBooleanValue(true);
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, not(containsString("intValue")));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
final MyDtoIgnoreField dtoObject = new MyDtoIgnoreField();
|
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
||||||
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, not(containsString("intValue")));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
mapper.addMixInAnnotations(String.class, MyMixInForString.class);
|
|
||||||
final MyDto dtoObject = new MyDto();
|
|
||||||
dtoObject.setBooleanValue(true);
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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"));
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
System.out.println(dtoAsString);
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException {
|
||||||
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
@Override
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility());
|
||||||
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();
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
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"));
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull();
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
mapper.setSerializationInclusion(Include.NON_NULL);
|
|
||||||
final MyDto dtoObject = new MyDto();
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tests - multiple entities to json
|
// tests - multiple entities to json
|
||||||
|
|
Loading…
Reference in New Issue