jackson work
This commit is contained in:
parent
5e462746e2
commit
502e405fc7
|
@ -0,0 +1,50 @@
|
||||||
|
package org.baeldung.jackson.ignore;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
public class MyDtoFieldNameChanged {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoFieldNameChanged() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDtoFieldNameChanged(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
@JsonProperty("strVal")
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -36,6 +36,17 @@ public class JacksonDeserializationUnitTest {
|
||||||
assertThat(readValue.getStringValue(), equalTo("a"));
|
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenNotAllFieldsHaveValuesInJson_whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException {
|
||||||
|
final String jsonAsString = "{\"stringValue\":\"a\",\"booleanValue\":true}";
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
final MyDto readValue = mapper.readValue(jsonAsString, MyDto.class);
|
||||||
|
|
||||||
|
assertNotNull(readValue);
|
||||||
|
assertThat(readValue.getStringValue(), equalTo("a"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test(expected = UnrecognizedPropertyException.class)
|
@Test(expected = UnrecognizedPropertyException.class)
|
||||||
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
|
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
|
||||||
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
|
||||||
|
@ -85,4 +96,3 @@ public class JacksonDeserializationUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.jackson.ignore.MyDto;
|
import org.baeldung.jackson.ignore.MyDto;
|
||||||
|
import org.baeldung.jackson.ignore.MyDtoFieldNameChanged;
|
||||||
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
|
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
|
||||||
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
|
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
|
||||||
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
|
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
|
||||||
|
@ -32,7 +33,7 @@ public class JacksonSerializationUnitTest {
|
||||||
// tests - single entity to json
|
// tests - single entity to json
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasOnlyDefaultValues_thenCorrect() throws JsonParseException, IOException {
|
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasOnlyDefaultValues_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 MyDtoIncludeNonDefault());
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ public class JacksonSerializationUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenOnlyNonDefaultValuesAreSerialized_whenDtoHasNonDefaultValue_thenCorrect() throws JsonParseException, IOException {
|
public final void givenOnlyNonDefaultValuesAreSerializedAndDtoHasNonDefaultValue_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
|
final MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
|
||||||
dtoObject.setBooleanValue(true);
|
dtoObject.setBooleanValue(true);
|
||||||
|
@ -52,6 +53,21 @@ public class JacksonSerializationUnitTest {
|
||||||
System.out.println(dtoAsString);
|
System.out.println(dtoAsString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenNameOfFieldIsChanged_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged();
|
||||||
|
dtoObject.setStringValue("a");
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
|
assertThat(dtoAsString, containsString("strVal"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ignore
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
@ -110,17 +126,18 @@ public class JacksonSerializationUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenTypeHasFilterThatIgnoresIntsOver10_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
public final void givenTypeHasFilterThatIgnoresNegativeInt_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
|
||||||
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
|
final PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
|
||||||
@Override
|
@Override
|
||||||
public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
|
public final void serializeAsField(final Object pojo, final JsonGenerator jgen, final SerializerProvider provider, final PropertyWriter writer) throws Exception {
|
||||||
if (include(writer)) {
|
if (include(writer)) {
|
||||||
if (writer.getName().equals("intValue")) {
|
if (!writer.getName().equals("intValue")) {
|
||||||
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
|
writer.serializeAsField(pojo, jgen, provider);
|
||||||
if (intValue < 10) {
|
return;
|
||||||
writer.serializeAsField(pojo, jgen, provider);
|
}
|
||||||
}
|
|
||||||
} else {
|
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
|
||||||
|
if (intValue >= 0) {
|
||||||
writer.serializeAsField(pojo, jgen, provider);
|
writer.serializeAsField(pojo, jgen, provider);
|
||||||
}
|
}
|
||||||
} else if (!jgen.canOmitFields()) { // since 2.3
|
} else if (!jgen.canOmitFields()) { // since 2.3
|
||||||
|
@ -141,7 +158,7 @@ public class JacksonSerializationUnitTest {
|
||||||
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
|
||||||
|
|
||||||
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
|
||||||
dtoObject.setIntValue(12);
|
dtoObject.setIntValue(-1);
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
|
final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);
|
||||||
|
|
Loading…
Reference in New Issue