diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java index 0fa3352000..88ee1cd673 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java @@ -14,13 +14,13 @@ import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; public class CustomCarDeserializer extends JsonDeserializer { - protected final Logger Logger = LoggerFactory.getLogger(getClass()); + private final Logger Logger = LoggerFactory.getLogger(getClass()); public CustomCarDeserializer() { } @Override - public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException, JsonProcessingException { + public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException { final Car car = new Car(); final ObjectCodec codec = parser.getCodec(); final JsonNode node = codec.readTree(parser); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java deleted file mode 100644 index 3ed5419e63..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import org.junit.Test; - -public abstract class Example { - - String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; - - public abstract String name(); - - @Test - public abstract void testExample() throws Exception; -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java deleted file mode 100644 index fbfd1a8190..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JavaToJsonExample extends Example -{ - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JavaToJsonExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = new Car("yellow", "renault"); - final String carAsString = objectMapper.writeValueAsString(car); - assertThat(carAsString, containsString("yellow")); - assertThat(carAsString, containsString("renault")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java deleted file mode 100644 index 76074b4f19..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.core.Version; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; - -public class JsonAdvancedCustomSerializeExample extends Example -{ - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonAdvancedCustomSerializeExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - public void testExample() throws Exception { - final ObjectMapper mapper = new ObjectMapper(); - final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); - serializerModule.addSerializer(Car.class, new CustomCarSerializer()); - mapper.registerModule(serializerModule); - final Car car = new Car("yellow", "renault"); - final String carJson = mapper.writeValueAsString(car); - assertThat(carJson, containsString("renault")); - assertThat(carJson, containsString("model")); - - final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); - deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer()); - mapper.registerModule(deserializerModule); - final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class); - assertNotNull(carResult); - assertThat(carResult.getColor(), equalTo("Black")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java deleted file mode 100644 index 9ac1b0df5f..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonAdvancedJsonNodeExample extends Example -{ - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonAdvancedJsonNodeExample() { } - - String LOCAL_JSON = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - public void testExample() throws Exception { - - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - final Car car = objectMapper.readValue(LOCAL_JSON, Car.class); - final JsonNode jsonNodeRoot = objectMapper.readTree(LOCAL_JSON); - final JsonNode jsonNodeYear = jsonNodeRoot.get("year"); - final String year = jsonNodeYear.asText(); - - assertNotNull(car); - assertThat(car.getColor(), equalTo("Black")); - assertThat(year, containsString("1970")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java deleted file mode 100644 index e25e7ce3e1..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import java.util.List; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonArrayExample extends Example { - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonArrayExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - class Response { - - public Response(final List cars) { - this.cars = cars; - } - - List cars; - - public List getCars() { - return cars; - } - - public void setCars(final List cars) { - this.cars = cars; - } - - } - - final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; - - @Override - @Test - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); - final Car[] cars = objectMapper.readValue(LOCAL_JSON, Car[].class); - for (final Car car : cars) { - assertNotNull(car); - assertThat(car.getType(), equalTo("BMW")); - } - final List listCar = objectMapper.readValue(LOCAL_JSON, new TypeReference>() { - - }); - for (final Car car : listCar) { - assertNotNull(car); - assertThat(car.getType(), equalTo("BMW")); - } - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java deleted file mode 100644 index 575b5d0f8c..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonDateExample extends Example { - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonDateExample() { - } - - @Override - public String name() { - return this.getClass().getName(); - } - - class Request { - Car car; - Date datePurchased; - public Car getCar() { - return car; - } - - public void setCar(final Car car) { - this.car = car; - } - - public Date getDatePurchased() { - return datePurchased; - } - - public void setDatePurchased(final Date datePurchased) { - this.datePurchased = datePurchased; - } - } - - @Override - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = new Car("yellow", "renault"); - final Request request = new Request(); - request.setCar(car); - request.setDatePurchased(new Date()); - final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z"); - objectMapper.setDateFormat(df); - final String carAsString = objectMapper.writeValueAsString(request); - assertNotNull(carAsString); - assertThat(carAsString, containsString("datePurchased")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java deleted file mode 100644 index 4a685489d2..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.junit.Assert.assertNotNull; - -import java.util.Map; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonMapExample extends Example { - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonMapExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - @Test - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - final Map map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference>() { - }); - assertNotNull(map); - for (final String key : map.keySet()) { - assertNotNull(key); - } - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java deleted file mode 100644 index e255956e6f..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonToJavaExample extends Example -{ - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonToJavaExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - @Test - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class); - assertNotNull(car); - assertThat(car.getColor(), containsString("Black")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java deleted file mode 100644 index 4b653c030b..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonToJsonNode extends Example -{ - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonToJsonNode() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - @Test - public void testExample() throws Exception { - final ObjectMapper objectMapper = new ObjectMapper(); - final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); - assertNotNull(jsonNode); - assertThat(jsonNode.get("color").asText(), containsString("Black")); - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestJavaReadWriteJsonExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestJavaReadWriteJsonExample.java new file mode 100644 index 0000000000..54ddf469c8 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestJavaReadWriteJsonExample.java @@ -0,0 +1,68 @@ +package com.baeldung.jackson.objectmapper; + +import com.baeldung.jackson.objectmapper.dto.Car; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +public class TestJavaReadWriteJsonExample { + final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; + final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; + + @Test + public void whenWriteJavaToJson_thanCorrect() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final Car car = new Car("yellow", "renault"); + final String carAsString = objectMapper.writeValueAsString(car); + assertThat(carAsString, containsString("yellow")); + assertThat(carAsString, containsString("renault")); + } + + @Test + public void whenReadJsonToJava_thanCorrect() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class); + assertNotNull(car); + assertThat(car.getColor(), containsString("Black")); + } + + @Test + public void whenReadJsonToJsonNode_thanCorrect() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); + assertNotNull(jsonNode); + assertThat(jsonNode.get("color").asText(), containsString("Black")); + } + + @Test + public void whenReadJsonToList_thanCorrect() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final List listCar = objectMapper.readValue(LOCAL_JSON, new TypeReference>() { + + }); + for (final Car car : listCar) { + assertNotNull(car); + assertThat(car.getType(), equalTo("BMW")); + } + } + + @Test + public void whenReadJsonToMap_thanCorrect() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final Map map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference>() { + }); + assertNotNull(map); + for (final String key : map.keySet()) { + assertNotNull(key); + } + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestSerializationDeserializationFeature.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestSerializationDeserializationFeature.java new file mode 100644 index 0000000000..96918f4c28 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/TestSerializationDeserializationFeature.java @@ -0,0 +1,84 @@ +package com.baeldung.jackson.objectmapper; + +import com.baeldung.jackson.objectmapper.dto.Car; +import com.baeldung.jackson.objectmapper.dto.Request; +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.junit.Test; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +public class TestSerializationDeserializationFeature { + final String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; + final String JSON_CAR = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; + final String JSON_ARRAY = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; + + @Test + public void whenFailOnUnkownPropertiesFalse_thanJsonReadCorrectly() throws Exception { + + final ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + final Car car = objectMapper.readValue(JSON_CAR, Car.class); + final JsonNode jsonNodeRoot = objectMapper.readTree(JSON_CAR); + final JsonNode jsonNodeYear = jsonNodeRoot.get("year"); + final String year = jsonNodeYear.asText(); + + assertNotNull(car); + assertThat(car.getColor(), equalTo("Black")); + assertThat(year, containsString("1970")); + } + + @Test + public void whenCustomSerializerDeserializer_thanReadWriteCorrect() throws Exception { + final ObjectMapper mapper = new ObjectMapper(); + final SimpleModule serializerModule = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); + serializerModule.addSerializer(Car.class, new CustomCarSerializer()); + mapper.registerModule(serializerModule); + final Car car = new Car("yellow", "renault"); + final String carJson = mapper.writeValueAsString(car); + assertThat(carJson, containsString("renault")); + assertThat(carJson, containsString("model")); + + final SimpleModule deserializerModule = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); + deserializerModule.addDeserializer(Car.class, new CustomCarDeserializer()); + mapper.registerModule(deserializerModule); + final Car carResult = mapper.readValue(EXAMPLE_JSON, Car.class); + assertNotNull(carResult); + assertThat(carResult.getColor(), equalTo("Black")); + } + + @Test + public void whenDateFormatSet_thanSerializedAsExpected() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final Car car = new Car("yellow", "renault"); + final Request request = new Request(); + request.setCar(car); + request.setDatePurchased(new Date()); + final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z"); + objectMapper.setDateFormat(df); + final String carAsString = objectMapper.writeValueAsString(request); + assertNotNull(carAsString); + assertThat(carAsString, containsString("datePurchased")); + } + + @Test + public void whenUseJavaArrayForJsonArrayTrue_thanJsonReadAsArray() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); + final Car[] cars = objectMapper.readValue(JSON_ARRAY, Car[].class); + for (final Car car : cars) { + assertNotNull(car); + assertThat(car.getType(), equalTo("BMW")); + } + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java new file mode 100644 index 0000000000..2a4cce0fdd --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/dto/Request.java @@ -0,0 +1,24 @@ +package com.baeldung.jackson.objectmapper.dto; + +import java.util.Date; + +public class Request { + Car car; + Date datePurchased; + + public Car getCar() { + return car; + } + + public void setCar(final Car car) { + this.car = car; + } + + public Date getDatePurchased() { + return datePurchased; + } + + public void setDatePurchased(final Date datePurchased) { + this.datePurchased = datePurchased; + } +} \ No newline at end of file