From f14f2af42171bf21a0b648c178a1adf1e3fce44b Mon Sep 17 00:00:00 2001 From: Ashanka Das Date: Sun, 17 Jul 2016 01:19:44 +0200 Subject: [PATCH 1/2] Refs #BAEL-17 code for objectmapper. Added JUnit Test cases. --- jackson/.classpath | 2 +- .../objectmapper/CustomCarSerializer.java | 2 +- .../jackson/objectmapper/Example.java | 7 +++ .../objectmapper/JavaToJsonExample.java | 27 ++++----- .../JsonAdvancedCustomSerializeExample.java | 30 +++++++++- .../JsonAdvancedJsonNodeExample.java | 27 ++++++++- .../objectmapper/JsonArrayExample.java | 25 ++++++++ .../jackson/objectmapper/JsonDateExample.java | 18 ++++++ .../jackson/objectmapper/JsonMapExample.java | 19 +++++- .../objectmapper/JsonParserExample.java | 59 ------------------- .../objectmapper/JsonToJavaExample.java | 44 ++++---------- .../jackson/objectmapper/JsonToJsonNode.java | 18 +++++- 12 files changed, 160 insertions(+), 118 deletions(-) delete mode 100644 jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonParserExample.java diff --git a/jackson/.classpath b/jackson/.classpath index 8ebf6d9c31..5efa587d72 100644 --- a/jackson/.classpath +++ b/jackson/.classpath @@ -29,7 +29,7 @@ - + diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java index 7d72dae106..5ae2717b89 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java @@ -16,7 +16,7 @@ public class CustomCarSerializer extends JsonSerializer public void serialize(final Car car, final JsonGenerator jsonGenerator, final SerializerProvider serializer) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); - jsonGenerator.writeStringField("car_brand", car.getType()); + jsonGenerator.writeStringField("model: ", car.getType()); jsonGenerator.writeEndObject(); } } diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java index fa5add9b86..2deac199f0 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java @@ -1,8 +1,15 @@ package com.baeldung.jackson.objectmapper; +import org.junit.Test; + public abstract class Example { + String EXAMPLE_JSON = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; + public abstract String name(); public abstract void execute(); + + @Test + public abstract void test() 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 index 783b8d42f7..e1f99ca2d4 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java @@ -1,5 +1,9 @@ package com.baeldung.jackson.objectmapper; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; + +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,8 +30,6 @@ public class JavaToJsonExample extends Example { final ObjectMapper objectMapper = new ObjectMapper(); final Car car = new Car("yellow", "renault"); - final Request request = new Request(); - request.setCar(car); final String carAsString = objectMapper.writeValueAsString(car); Logger.debug(carAsString); } @@ -37,18 +39,13 @@ public class JavaToJsonExample extends Example } } - class Request - { - Car car; - - public Car getCar() - { - return car; - } - - public void setCar(final Car car) - { - this.car = car; - } + @Override + @Test + public void test() 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 index 735dada1d9..00a90209de 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java @@ -1,5 +1,11 @@ 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.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,8 +21,6 @@ public class JsonAdvancedCustomSerializeExample extends Example public JsonAdvancedCustomSerializeExample() { } - String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; - @Override public String name() { @@ -49,7 +53,7 @@ public class JsonAdvancedCustomSerializeExample extends Example module.addDeserializer(Car.class, new CustomCarDeserializer()); mapper = new ObjectMapper(); mapper.registerModule(module); - final Car car = mapper.readValue(json, Car.class); + final Car car = mapper.readValue(EXAMPLE_JSON, Car.class); Logger.debug("car type = " + car.getType()); Logger.debug("car color = " + car.getColor()); } @@ -58,4 +62,24 @@ public class JsonAdvancedCustomSerializeExample extends Example Logger.error(e.toString()); } } + + @Override + @Test + public void test() 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 index 2e05aba235..c6482ca708 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java @@ -1,7 +1,13 @@ 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 java.io.StringWriter; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,7 +24,7 @@ public class JsonAdvancedJsonNodeExample extends Example public JsonAdvancedJsonNodeExample() { } - String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; + String LOCAL_JSON = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }"; @Override public String name() @@ -34,8 +40,8 @@ public class JsonAdvancedJsonNodeExample extends Example { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - final Car car = objectMapper.readValue(jsonString, Car.class); - final JsonNode jsonNodeRoot = objectMapper.readTree(jsonString); + 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(); Logger.debug("Year = " + year); @@ -53,4 +59,19 @@ public class JsonAdvancedJsonNodeExample extends Example } } + @Override + @Test + public void test() 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 index 40327ec787..4032e75681 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java @@ -1,7 +1,12 @@ 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; @@ -80,4 +85,24 @@ public class JsonArrayExample extends Example { } + final String LOCAL_JSON = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"BMW\" }]"; + + @Override + @Test + public void test() 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 index 3b28e03344..78c89b9996 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java @@ -1,5 +1,9 @@ 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; @@ -58,4 +62,18 @@ public class JsonDateExample extends Example { this.datePurchased = datePurchased; } } + + @Override + public void test() 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 index 2f8f3c7943..108c31b069 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java @@ -1,7 +1,10 @@ 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; @@ -24,10 +27,10 @@ public class JsonMapExample extends Example { public void execute() { final ObjectMapper objectMapper = new ObjectMapper(); - final String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; try { - final Map map = objectMapper.readValue(json, new TypeReference>(){}); + final Map map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference>() { + }); for(final String key : map.keySet()) { Logger.debug("key = " + key + " | value = " + map.get(key)); @@ -38,4 +41,16 @@ public class JsonMapExample extends Example { Logger.error(e.toString()); } } + + @Override + @Test + public void test() 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/JsonParserExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonParserExample.java deleted file mode 100644 index 8a2263b74b..0000000000 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonParserExample.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.baeldung.jackson.objectmapper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baeldung.jackson.objectmapper.dto.Car; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; - -public class JsonParserExample extends Example { - - protected final Logger Logger = LoggerFactory.getLogger(getClass()); - - public JsonParserExample() { } - - @Override - public String name() - { - return this.getClass().getName(); - } - - @Override - public void execute() - { - Logger.debug("Executing: "+name()); - final String carJson = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; - final JsonFactory factory = new JsonFactory(); - JsonParser parser; - try - { - final Car car = new Car(); - parser = factory.createParser(carJson); - while(!parser.isClosed()) - { - JsonToken jsonToken = parser.nextToken(); - Logger.debug("jsonToken = " + jsonToken); - - if(JsonToken.FIELD_NAME.equals(jsonToken)){ - final String fieldName = parser.getCurrentName(); - System.out.println(fieldName); - - jsonToken = parser.nextToken(); - - if("color".equals(fieldName)){ - car.setColor(parser.getValueAsString()); - } else if ("type".equals(fieldName)){ - car.setType(parser.getValueAsString()); - } - } - } - Logger.debug("car:"+car.getColor()); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } -} diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java index 1fe56e49a3..166dd695a2 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java @@ -1,10 +1,14 @@ 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.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonToJavaExample extends Example @@ -13,8 +17,6 @@ public class JsonToJavaExample extends Example public JsonToJavaExample() { } - String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; - @Override public String name() { @@ -28,7 +30,7 @@ public class JsonToJavaExample extends Example try { final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = objectMapper.readValue(json, Car.class); + final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class); Logger.debug("Color = " + car.getColor()); Logger.debug("Type = " + car.getType()); } @@ -36,34 +38,14 @@ public class JsonToJavaExample extends Example { Logger.error(e.toString()); } - - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); - - final String jsonCar = "\"car\" : { \"color\" : \"Red\", \"type\" : \"FIAT\" }"; - final Response response = objectMapper.readValue(jsonCar, Response.class); - - Logger.debug("response: "+response); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } } - class Response { - - Car car; - - public Car getCar() { - return car; - } - - public void setCars(final Car car) { - this.car = car; - } - + @Override + @Test + public void test() 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 index 3e41e4c785..cda90bec09 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java @@ -1,5 +1,10 @@ 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; @@ -12,8 +17,6 @@ public class JsonToJsonNode extends Example public JsonToJsonNode() { } - String jsonString = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; - @Override public String name() { @@ -27,7 +30,7 @@ public class JsonToJsonNode extends Example try { final ObjectMapper objectMapper = new ObjectMapper(); - final JsonNode jsonNode = objectMapper.readTree(jsonString); + final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); Logger.debug(jsonNode.get("color").asText()); } catch (final Exception e) @@ -35,4 +38,13 @@ public class JsonToJsonNode extends Example Logger.error(e.toString()); } } + + @Override + @Test + public void test() throws Exception { + final ObjectMapper objectMapper = new ObjectMapper(); + final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); + assertNotNull(jsonNode); + assertThat(jsonNode.get("color").asText(), containsString("Black")); + } } From eaa32bdb413d034dcbb350f9d8201999b9b2de5a Mon Sep 17 00:00:00 2001 From: Ashanka Das Date: Sun, 17 Jul 2016 01:52:45 +0200 Subject: [PATCH 2/2] Refs #BAEL-17 : Test objectMapper. --- .../jackson/objectmapper/Example.java | 4 +- .../objectmapper/JavaToJsonExample.java | 20 +-------- .../JsonAdvancedCustomSerializeExample.java | 40 +----------------- .../JsonAdvancedJsonNodeExample.java | 34 +-------------- .../objectmapper/JsonArrayExample.java | 42 +------------------ .../jackson/objectmapper/JsonDateExample.java | 19 +-------- .../jackson/objectmapper/JsonMapExample.java | 21 +--------- .../objectmapper/JsonToJavaExample.java | 19 +-------- .../jackson/objectmapper/JsonToJsonNode.java | 18 +------- 9 files changed, 9 insertions(+), 208 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java index 2deac199f0..3ed5419e63 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java @@ -8,8 +8,6 @@ public abstract class Example { public abstract String name(); - public abstract void execute(); - @Test - public abstract void test() throws Exception; + 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 index e1f99ca2d4..fbfd1a8190 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java @@ -3,7 +3,6 @@ package com.baeldung.jackson.objectmapper; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertThat; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,24 +23,7 @@ public class JavaToJsonExample extends Example } @Override - public void execute() - { - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = new Car("yellow", "renault"); - final String carAsString = objectMapper.writeValueAsString(car); - Logger.debug(carAsString); - } - catch(final Exception e) - { - Logger.error(e.toString()); - } - } - - @Override - @Test - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); final Car car = new Car("yellow", "renault"); final String carAsString = objectMapper.writeValueAsString(car); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java index 00a90209de..76074b4f19 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java @@ -5,7 +5,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,44 +27,7 @@ public class JsonAdvancedCustomSerializeExample extends Example } @Override - public void execute() - { - Logger.debug("Executing: "+name()); - try - { - ObjectMapper mapper = new ObjectMapper(); - final SimpleModule module = new SimpleModule("CustomSerializer", new Version(1, 0, 0, null, null, null)); - module.addSerializer(Car.class, new CustomCarSerializer()); - mapper = new ObjectMapper(); - mapper.registerModule(module); - final Car car = new Car("yellow", "renault"); - final String carJson = mapper.writeValueAsString(car); - Logger.debug("car as json = " + carJson); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - try - { - ObjectMapper mapper = new ObjectMapper(); - final SimpleModule module = new SimpleModule("CustomCarDeserializer", new Version(1, 0, 0, null, null, null)); - module.addDeserializer(Car.class, new CustomCarDeserializer()); - mapper = new ObjectMapper(); - mapper.registerModule(module); - final Car car = mapper.readValue(EXAMPLE_JSON, Car.class); - Logger.debug("car type = " + car.getType()); - Logger.debug("car color = " + car.getColor()); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - - @Override - @Test - public void test() throws Exception { + 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()); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java index c6482ca708..9ac1b0df5f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java @@ -5,9 +5,6 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; -import java.io.StringWriter; - -import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,7 +12,6 @@ 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; -import com.fasterxml.jackson.databind.SerializationFeature; public class JsonAdvancedJsonNodeExample extends Example { @@ -33,35 +29,7 @@ public class JsonAdvancedJsonNodeExample extends Example } @Override - public void execute() - { - Logger.debug("Executing: "+name()); - try - { - 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(); - Logger.debug("Year = " + year); - Logger.debug("Color = " + car.getColor()); - Logger.debug("Type = " + car.getType()); - - objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); - final StringWriter string = new StringWriter(); - objectMapper.writeValue(string, car); - Logger.debug("Car JSON is:"+string); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - - @Override - @Test - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java index 4032e75681..e25e7ce3e1 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java @@ -27,46 +27,6 @@ public class JsonArrayExample extends Example { return this.getClass().getName(); } - @Override - public void execute() - { - Logger.debug("Executing: "+name()); - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); - - final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]"; - final Car[] cars = objectMapper.readValue(jsonCarArray, Car[].class); - for(final Car car : cars) - { - Logger.debug("Color = " + car.getColor()); - Logger.debug("Type = " + car.getType()); - } - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); - - final String jsonCarArray = "[{ \"color\" : \"Black\", \"type\" : \"BMW\" }, { \"color\" : \"Red\", \"type\" : \"FIAT\" }]"; - final List listCar = objectMapper.readValue(jsonCarArray, new TypeReference>(){}); - for(final Car car : listCar) - { - Logger.debug("Color = " + car.getColor()); - Logger.debug("Type = " + car.getType()); - } - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - class Response { public Response(final List cars) { @@ -89,7 +49,7 @@ public class JsonArrayExample extends Example { @Override @Test - public void test() throws Exception { + 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); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java index 78c89b9996..575b5d0f8c 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java @@ -26,23 +26,6 @@ public class JsonDateExample extends Example { return this.getClass().getName(); } - @Override - public void execute() { - Logger.debug("Executing: " + name()); - try { - final Car car = new Car("yellow", "renault"); - final Request request = new Request(); - request.setCar(car); - request.setDatePurchased(new Date()); - final ObjectMapper objectMapper = new ObjectMapper(); - final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm a z"); - objectMapper.setDateFormat(df); - final String carAsString = objectMapper.writeValueAsString(request); - Logger.debug(carAsString); - } catch (final Exception e) { - Logger.error(e.toString()); - } - } class Request { Car car; Date datePurchased; @@ -64,7 +47,7 @@ public class JsonDateExample extends Example { } @Override - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); final Car car = new Car("yellow", "renault"); final Request request = new Request(); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java index 108c31b069..4a685489d2 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java @@ -23,28 +23,9 @@ public class JsonMapExample extends Example { return this.getClass().getName(); } - @Override - public void execute() - { - final ObjectMapper objectMapper = new ObjectMapper(); - try - { - final Map map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference>() { - }); - for(final String key : map.keySet()) - { - Logger.debug("key = " + key + " | value = " + map.get(key)); - } - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - @Override @Test - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); final Map map = objectMapper.readValue(EXAMPLE_JSON, new TypeReference>() { }); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java index 166dd695a2..e255956e6f 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJavaExample.java @@ -23,26 +23,9 @@ public class JsonToJavaExample extends Example return this.getClass().getName(); } - @Override - public void execute() - { - Logger.debug("Executing: "+name()); - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class); - Logger.debug("Color = " + car.getColor()); - Logger.debug("Type = " + car.getType()); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - @Override @Test - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); final Car car = objectMapper.readValue(EXAMPLE_JSON, Car.class); assertNotNull(car); diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java index cda90bec09..4b653c030b 100644 --- a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java +++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonToJsonNode.java @@ -23,25 +23,9 @@ public class JsonToJsonNode extends Example return this.getClass().getName(); } - @Override - public void execute() - { - Logger.debug("Executing: "+name()); - try - { - final ObjectMapper objectMapper = new ObjectMapper(); - final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); - Logger.debug(jsonNode.get("color").asText()); - } - catch (final Exception e) - { - Logger.error(e.toString()); - } - } - @Override @Test - public void test() throws Exception { + public void testExample() throws Exception { final ObjectMapper objectMapper = new ObjectMapper(); final JsonNode jsonNode = objectMapper.readTree(EXAMPLE_JSON); assertNotNull(jsonNode);