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);