diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDto.java b/jackson/src/test/java/dtos/MyDto.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDto.java rename to jackson/src/test/java/dtos/MyDto.java index 32fcfd4ba6..bf140f93d8 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDto.java +++ b/jackson/src/test/java/dtos/MyDto.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; public class MyDto { diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java b/jackson/src/test/java/dtos/MyDtoFieldNameChanged.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java rename to jackson/src/test/java/dtos/MyDtoFieldNameChanged.java index 5b98547ae3..f9537e5ad0 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoFieldNameChanged.java +++ b/jackson/src/test/java/dtos/MyDtoFieldNameChanged.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreField.java b/jackson/src/test/java/dtos/MyDtoIgnoreField.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreField.java rename to jackson/src/test/java/dtos/MyDtoIgnoreField.java index bdbc694207..34cccc630c 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreField.java +++ b/jackson/src/test/java/dtos/MyDtoIgnoreField.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreFieldByName.java b/jackson/src/test/java/dtos/MyDtoIgnoreFieldByName.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreFieldByName.java rename to jackson/src/test/java/dtos/MyDtoIgnoreFieldByName.java index 0b4378db75..affb9330d1 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreFieldByName.java +++ b/jackson/src/test/java/dtos/MyDtoIgnoreFieldByName.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreNull.java b/jackson/src/test/java/dtos/MyDtoIgnoreNull.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreNull.java rename to jackson/src/test/java/dtos/MyDtoIgnoreNull.java index db041cde10..33ec587711 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreNull.java +++ b/jackson/src/test/java/dtos/MyDtoIgnoreNull.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreType.java b/jackson/src/test/java/dtos/MyDtoIgnoreType.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreType.java rename to jackson/src/test/java/dtos/MyDtoIgnoreType.java index a159015f08..08ca86766a 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreType.java +++ b/jackson/src/test/java/dtos/MyDtoIgnoreType.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreUnkown.java b/jackson/src/test/java/dtos/MyDtoIgnoreUnkown.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreUnkown.java rename to jackson/src/test/java/dtos/MyDtoIgnoreUnkown.java index 5f0de0c7fe..d1f9960c83 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIgnoreUnkown.java +++ b/jackson/src/test/java/dtos/MyDtoIgnoreUnkown.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIncludeNonDefault.java b/jackson/src/test/java/dtos/MyDtoIncludeNonDefault.java similarity index 95% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIncludeNonDefault.java rename to jackson/src/test/java/dtos/MyDtoIncludeNonDefault.java index 6d1b76a39d..1d54943894 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoIncludeNonDefault.java +++ b/jackson/src/test/java/dtos/MyDtoIncludeNonDefault.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; diff --git a/jackson/src/test/java/dtos/MyDtoNoAccessors.java b/jackson/src/test/java/dtos/MyDtoNoAccessors.java new file mode 100644 index 0000000000..3988f714b2 --- /dev/null +++ b/jackson/src/test/java/dtos/MyDtoNoAccessors.java @@ -0,0 +1,21 @@ +package dtos; + +public class MyDtoNoAccessors { + + private String stringValue; + private int intValue; + private boolean booleanValue; + + public MyDtoNoAccessors() { + super(); + } + + public MyDtoNoAccessors(final String stringValue, final int intValue, final boolean booleanValue) { + super(); + + this.stringValue = stringValue; + this.intValue = intValue; + this.booleanValue = booleanValue; + } + +} diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoWithFilter.java b/jackson/src/test/java/dtos/MyDtoWithFilter.java similarity index 96% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoWithFilter.java rename to jackson/src/test/java/dtos/MyDtoWithFilter.java index 2492cb82cb..fe40c86728 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyDtoWithFilter.java +++ b/jackson/src/test/java/dtos/MyDtoWithFilter.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonFilter; diff --git a/jackson/src/test/java/org/baeldung/jackson/ignore/MyMixInForString.java b/jackson/src/test/java/dtos/MyMixInForString.java similarity index 75% rename from jackson/src/test/java/org/baeldung/jackson/ignore/MyMixInForString.java rename to jackson/src/test/java/dtos/MyMixInForString.java index 1439cca793..0c93b44d79 100644 --- a/jackson/src/test/java/org/baeldung/jackson/ignore/MyMixInForString.java +++ b/jackson/src/test/java/dtos/MyMixInForString.java @@ -1,4 +1,4 @@ -package org.baeldung.jackson.ignore; +package dtos; import com.fasterxml.jackson.annotation.JsonIgnoreType; diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java index f3063e9ebf..6a0c846696 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonCollectionDeserializationUnitTest.java @@ -7,7 +7,6 @@ import java.io.IOException; import java.util.LinkedHashMap; import java.util.List; -import org.baeldung.jackson.ignore.MyDto; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; @@ -17,6 +16,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.CollectionType; import com.google.common.collect.Lists; +import dtos.MyDto; + public class JacksonCollectionDeserializationUnitTest { // tests - json to multiple entity 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 2965b732c2..44de65e09e 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonDeserializationUnitTest.java @@ -6,19 +6,21 @@ import static org.junit.Assert.assertThat; import java.io.IOException; -import org.baeldung.jackson.ignore.MyDto; -import org.baeldung.jackson.ignore.MyDtoIgnoreUnkown; import org.junit.Test; +import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; -public class JacksonDeserializationUnitTest { +import dtos.MyDto; +import dtos.MyDtoIgnoreUnkown; - // tests - json to single entity +public class JacksonDeserializationUnitTest { @Test public final void whenDeserializingAJsonToAClass_thenCorrect() throws JsonParseException, JsonMappingException, IOException { @@ -43,6 +45,8 @@ public class JacksonDeserializationUnitTest { assertThat(readValue.isBooleanValue(), equalTo(true)); } + // tests - json with unknown fields + @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\"}"; @@ -91,4 +95,39 @@ public class JacksonDeserializationUnitTest { assertThat(readValue.getIntValue(), equalTo(1)); } + // to JsonNode + + @Test + public final void whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode actualObj = mapper.readTree(jsonString); + + assertNotNull(actualObj); + } + + @Test + public final void givenUsingLowLevelDetails_whenParsingJsonStringIntoJsonNode_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + + final ObjectMapper mapper = new ObjectMapper(); + final JsonFactory factory = mapper.getFactory(); + final JsonParser parser = factory.createParser(jsonString); + final JsonNode actualObj = mapper.readTree(parser); + + assertNotNull(actualObj); + } + + @Test + public final void givenTheJsonNode_whenRetrievingDataFromId_thenCorrect() throws JsonParseException, IOException { + final String jsonString = "{\"k1\":\"v1\",\"k2\":\"v2\"}"; + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode actualObj = mapper.readTree(jsonString); + + // When + final JsonNode jsonNode1 = actualObj.get("k1"); + assertThat(jsonNode1.textValue(), equalTo("v1")); + } + } 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 f144be5e6a..177ef19afe 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonSerializationUnitTest.java @@ -7,14 +7,6 @@ import static org.junit.Assert.assertThat; 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.MyDtoIgnoreNull; -import org.baeldung.jackson.ignore.MyDtoIncludeNonDefault; -import org.baeldung.jackson.ignore.MyDtoWithFilter; -import org.baeldung.jackson.ignore.MyMixInForString; import org.junit.Test; import com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -31,6 +23,15 @@ import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; import com.google.common.collect.Lists; +import dtos.MyDto; +import dtos.MyDtoFieldNameChanged; +import dtos.MyDtoIgnoreField; +import dtos.MyDtoIgnoreFieldByName; +import dtos.MyDtoIgnoreNull; +import dtos.MyDtoIncludeNonDefault; +import dtos.MyDtoWithFilter; +import dtos.MyMixInForString; + public class JacksonSerializationUnitTest { // tests - single entity to json @@ -173,7 +174,7 @@ public class JacksonSerializationUnitTest { } @Test - public final void givenIgnoringNullFieldsOnClass_whenSerializingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException { + public final void givenIgnoringNullFieldsOnClass_whenWritingObjectWithNullField_thenFieldIsIgnored() throws JsonProcessingException { final ObjectMapper mapper = new ObjectMapper(); final MyDtoIgnoreNull dtoObject = new MyDtoIgnoreNull(); @@ -186,7 +187,7 @@ public class JacksonSerializationUnitTest { } @Test - public final void givenIgnoringNullFieldsGlobally_whenSerializingObjectWithNullField_thenFieldIsIgnroed() throws JsonProcessingException { + public final void givenIgnoringNullFieldsGlobally_whenWritingObjectWithNullField_thenIgnored() throws JsonProcessingException { final ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); final MyDto dtoObject = new MyDto();