diff --git a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java index d9bfe6f8f6..668eea3fcc 100644 --- a/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java +++ b/jackson/src/test/java/org/baeldung/jackson/dtos/MyDto.java @@ -44,4 +44,11 @@ public class MyDto { this.booleanValue = booleanValue; } + // + + @Override + public String toString() { + return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]"; + } + } diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java index d1761b30ed..c164b00e4a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoAccessLevel.java @@ -4,17 +4,19 @@ public class MyDtoAccessLevel { private String stringValue; int intValue; + protected float floatValue; public boolean booleanValue; public MyDtoAccessLevel() { super(); } - public MyDtoAccessLevel(final String stringValue, final int intValue, final boolean booleanValue) { + public MyDtoAccessLevel(final String stringValue, final int intValue, final float floatValue, final boolean booleanValue) { super(); this.stringValue = stringValue; this.intValue = intValue; + this.floatValue = floatValue; this.booleanValue = booleanValue; } diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java deleted file mode 100644 index 0c5027ac24..0000000000 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetterImplicitDeserialization.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.baeldung.jackson.field; - -public class MyDtoGetterImplicitDeserialization { - - private String stringValue; - - public MyDtoGetterImplicitDeserialization() { - super(); - } - - public MyDtoGetterImplicitDeserialization(final String stringValue) { - super(); - - this.stringValue = stringValue; - } - - // API - - public String getStringValue() { - return stringValue; - } - -} diff --git a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java similarity index 69% rename from jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java rename to jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java index b0725b9d65..0f15b2fcc3 100644 --- a/jackson/src/test/java/org/baeldung/jackson/field/MyDtoGetter.java +++ b/jackson/src/test/java/org/baeldung/jackson/field/MyDtoWithGetter.java @@ -1,15 +1,15 @@ package org.baeldung.jackson.field; -public class MyDtoGetter { +public class MyDtoWithGetter { private String stringValue; private int intValue; - public MyDtoGetter() { + public MyDtoWithGetter() { super(); } - public MyDtoGetter(final String stringValue, final int intValue) { + public MyDtoWithGetter(final String stringValue, final int intValue) { super(); this.stringValue = stringValue; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java index 44c2ba534d..8e67144d43 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonFieldUnitTest.java @@ -9,9 +9,8 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import org.baeldung.jackson.field.MyDtoAccessLevel; -import org.baeldung.jackson.field.MyDtoGetter; -import org.baeldung.jackson.field.MyDtoGetterImplicitDeserialization; import org.baeldung.jackson.field.MyDtoSetter; +import org.baeldung.jackson.field.MyDtoWithGetter; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; @@ -30,6 +29,7 @@ public class JacksonFieldUnitTest { final String dtoAsString = mapper.writeValueAsString(dtoObject); assertThat(dtoAsString, not(containsString("stringValue"))); assertThat(dtoAsString, not(containsString("intValue"))); + assertThat(dtoAsString, not(containsString("floatValue"))); assertThat(dtoAsString, containsString("booleanValue")); System.out.println(dtoAsString); } @@ -38,7 +38,7 @@ public class JacksonFieldUnitTest { public final void givenDifferentAccessLevels_whenGetterAdded_thenSerializable() throws JsonProcessingException { final ObjectMapper mapper = new ObjectMapper(); - final MyDtoGetter dtoObject = new MyDtoGetter(); + final MyDtoWithGetter dtoObject = new MyDtoWithGetter(); final String dtoAsString = mapper.writeValueAsString(dtoObject); assertThat(dtoAsString, containsString("stringValue")); @@ -51,7 +51,7 @@ public class JacksonFieldUnitTest { final String jsonAsString = "{\"stringValue\":\"dtoString\"}"; final ObjectMapper mapper = new ObjectMapper(); - final MyDtoGetterImplicitDeserialization dtoObject = mapper.readValue(jsonAsString, MyDtoGetterImplicitDeserialization.class); + final MyDtoWithGetter dtoObject = mapper.readValue(jsonAsString, MyDtoWithGetter.class); assertNotNull(dtoObject); assertThat(dtoObject.getStringValue(), equalTo("dtoString")); diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java index d0e0426ff0..64614fb854 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationIgnoreUnitTest.java @@ -225,21 +225,25 @@ public class JacksonSerializationIgnoreUnitTest { } @Test - public final void givenAllowingMapObjectWithNullKey_whenWritingMapObjectWithNullKey_thenAllowed() throws JsonProcessingException { + public final void givenAllowingMapObjectWithNullKey_whenWriting_thenCorrect() throws JsonProcessingException { final ObjectMapper mapper = new ObjectMapper(); mapper.getSerializerProvider().setNullKeySerializer(new MyDtoNullKeySerializer()); - final MyDto dtoObject = new MyDto(); - dtoObject.setStringValue("dtoObjectString"); + final MyDto dtoObject1 = new MyDto(); + dtoObject1.setStringValue("dtoObjectString1"); + final MyDto dtoObject2 = new MyDto(); + dtoObject2.setStringValue("dtoObjectString2"); final Map dtoMap = new HashMap(); - dtoMap.put(null, dtoObject); + dtoMap.put(null, dtoObject1); + dtoMap.put("obj2", dtoObject2); final String dtoMapAsString = mapper.writeValueAsString(dtoMap); - assertThat(dtoMapAsString, containsString("")); - assertThat(dtoMapAsString, containsString("dtoObjectString")); System.out.println(dtoMapAsString); + assertThat(dtoMapAsString, containsString("\"\"")); + assertThat(dtoMapAsString, containsString("dtoObjectString1")); + assertThat(dtoMapAsString, containsString("obj2")); } @Test