From 502e405fc73ef3599120b02d5d5c7e3c6ce06c7d Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 22 Dec 2013 11:44:54 +0200 Subject: [PATCH] jackson work --- .../jackson/ignore/MyDtoFieldNameChanged.java | 50 +++++++++++++++++++ .../test/JacksonDeserializationUnitTest.java | 12 ++++- .../test/JacksonSerializationUnitTest.java | 37 ++++++++++---- 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java b/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java new file mode 100644 index 0000000000..5b98547ae3 --- /dev/null +++ b/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java @@ -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; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java index 07c960ba66..a0df4056af 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java @@ -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 { } } - diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java index 4a25f33b87..04f1cf066b 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -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);