jackson work

This commit is contained in:
eugenp 2013-12-22 11:44:54 +02:00
parent 5e462746e2
commit 502e405fc7
3 changed files with 88 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -36,6 +36,17 @@ public class JacksonDeserializationUnitTest {
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)
public final void givenJsonHasUnkownValues_whenDeserializingAJsonToAClass_thenExceptionIsThrown() throws JsonParseException, JsonMappingException, IOException {
final String jsonAsString = "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":true,\"stringValue2\":\"something\"}";
@ -85,4 +96,3 @@ public class JacksonDeserializationUnitTest {
}
}

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.util.List;
import org.baeldung.jackson.ignore.MyDto;
import org.baeldung.jackson.ignore.MyDtoFieldNameChanged;
import org.baeldung.jackson.ignore.MyDtoIgnoreField;
import org.baeldung.jackson.ignore.MyDtoIgnoreFieldByName;
import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault;
@ -32,7 +33,7 @@ public class JacksonSerializationUnitTest {
// tests - single entity to json
@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 String dtoAsString = mapper.writeValueAsString(new MyDtoIncludeNonDefault());
@ -41,7 +42,7 @@ public class JacksonSerializationUnitTest {
}
@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 MyDtoIncludeNonDefault dtoObject = new MyDtoIncludeNonDefault();
dtoObject.setBooleanValue(true);
@ -52,6 +53,21 @@ public class JacksonSerializationUnitTest {
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
public final void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
final ObjectMapper mapper = new ObjectMapper();
@ -110,17 +126,18 @@ public class JacksonSerializationUnitTest {
}
@Test
public final void givenTypeHasFilterThatIgnoresIntsOver10_whenDtoIsSerialized_thenCorrect() throws JsonParseException, IOException {
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")) {
final int intValue = ((MyDtoWithFilter) pojo).getIntValue();
if (intValue < 10) {
writer.serializeAsField(pojo, jgen, provider);
}
} else {
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
@ -141,7 +158,7 @@ public class JacksonSerializationUnitTest {
final FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);
final MyDtoWithFilter dtoObject = new MyDtoWithFilter();
dtoObject.setIntValue(12);
dtoObject.setIntValue(-1);
final ObjectMapper mapper = new ObjectMapper();
final String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);