diff --git a/jackson/pom.xml b/jackson/pom.xml
index c7cd172757..17b0ac507e 100644
--- a/jackson/pom.xml
+++ b/jackson/pom.xml
@@ -109,6 +109,31 @@
${mockito.version}
test
+
+
+
+
+ org.slf4j
+ slf4j-api
+ ${org.slf4j.version}
+
+
+ ch.qos.logback
+ logback-classic
+ ${logback.version}
+
+
+
+ org.slf4j
+ jcl-over-slf4j
+ ${org.slf4j.version}
+
+
+
+ org.slf4j
+ log4j-over-slf4j
+ ${org.slf4j.version}
+
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java
new file mode 100644
index 0000000000..87eedbaebc
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarDeserializer.java
@@ -0,0 +1,40 @@
+package com.baeldung.jackson.objectmapper;
+
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.baeldung.jackson.objectmapper.dto.Car;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.ObjectCodec;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+
+public class CustomCarDeserializer extends JsonDeserializer
+{
+ protected final Logger Logger = LoggerFactory.getLogger(getClass());
+
+ public CustomCarDeserializer() { }
+
+ @Override
+ public Car deserialize(final JsonParser parser, final DeserializationContext deserializer) throws IOException, JsonProcessingException
+ {
+ final Car car = new Car();
+ final ObjectCodec codec = parser.getCodec();
+ final JsonNode node = codec.readTree(parser);
+ try
+ {
+ final JsonNode colorNode = node.get("color");
+ final String color = colorNode.asText();
+ car.setColor(color);
+ }
+ catch(final Exception e)
+ {
+ Logger.debug("101_parse_exeption: unknown json.");
+ }
+ return car;
+ }
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java
new file mode 100644
index 0000000000..7d72dae106
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/CustomCarSerializer.java
@@ -0,0 +1,22 @@
+package com.baeldung.jackson.objectmapper;
+
+import java.io.IOException;
+
+import com.baeldung.jackson.objectmapper.dto.Car;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+
+public class CustomCarSerializer extends JsonSerializer
+{
+ public CustomCarSerializer() { }
+
+ @Override
+ public void serialize(final Car car, final JsonGenerator jsonGenerator, final SerializerProvider serializer) throws IOException, JsonProcessingException
+ {
+ jsonGenerator.writeStartObject();
+ jsonGenerator.writeStringField("car_brand", 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
new file mode 100644
index 0000000000..fa5add9b86
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/Example.java
@@ -0,0 +1,8 @@
+package com.baeldung.jackson.objectmapper;
+
+public abstract class Example {
+
+ public abstract String name();
+
+ public abstract void execute();
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java
new file mode 100644
index 0000000000..783b8d42f7
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JavaToJsonExample.java
@@ -0,0 +1,54 @@
+package com.baeldung.jackson.objectmapper;
+
+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 execute()
+ {
+ try
+ {
+ 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);
+ }
+ catch(final Exception e)
+ {
+ Logger.error(e.toString());
+ }
+ }
+
+ class Request
+ {
+ Car car;
+
+ public Car getCar()
+ {
+ return car;
+ }
+
+ public void setCar(final Car car)
+ {
+ this.car = 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
new file mode 100644
index 0000000000..735dada1d9
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedCustomSerializeExample.java
@@ -0,0 +1,61 @@
+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.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() { }
+
+ String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
+
+ @Override
+ public String name()
+ {
+ return this.getClass().getName();
+ }
+
+ @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(json, Car.class);
+ Logger.debug("car type = " + car.getType());
+ Logger.debug("car color = " + car.getColor());
+ }
+ catch (final Exception e)
+ {
+ Logger.error(e.toString());
+ }
+ }
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java
new file mode 100644
index 0000000000..2e05aba235
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonAdvancedJsonNodeExample.java
@@ -0,0 +1,56 @@
+package com.baeldung.jackson.objectmapper;
+
+import java.io.StringWriter;
+
+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;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+public class JsonAdvancedJsonNodeExample extends Example
+{
+
+ protected final Logger Logger = LoggerFactory.getLogger(getClass());
+
+ public JsonAdvancedJsonNodeExample() { }
+
+ String jsonString = "{ \"color\" : \"Black\", \"type\" : \"Fiat\", \"year\" : \"1970\" }";
+
+ @Override
+ public String name()
+ {
+ return this.getClass().getName();
+ }
+
+ @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(jsonString, Car.class);
+ final JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);
+ 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());
+ }
+ }
+
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java
new file mode 100644
index 0000000000..40327ec787
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonArrayExample.java
@@ -0,0 +1,83 @@
+package com.baeldung.jackson.objectmapper;
+
+import java.util.List;
+
+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();
+ }
+
+ @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) {
+ this.cars = cars;
+ }
+
+ List cars;
+
+ public List getCars() {
+ return cars;
+ }
+
+ public void setCars(final List cars) {
+ this.cars = cars;
+ }
+
+ }
+
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java
new file mode 100644
index 0000000000..3b28e03344
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonDateExample.java
@@ -0,0 +1,61 @@
+package com.baeldung.jackson.objectmapper;
+
+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();
+ }
+
+ @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;
+ 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;
+ }
+ }
+}
diff --git a/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java
new file mode 100644
index 0000000000..2f8f3c7943
--- /dev/null
+++ b/jackson/src/test/java/com/baeldung/jackson/objectmapper/JsonMapExample.java
@@ -0,0 +1,41 @@
+package com.baeldung.jackson.objectmapper;
+
+import java.util.Map;
+
+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
+ public void execute()
+ {
+ final ObjectMapper objectMapper = new ObjectMapper();
+ final String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
+ try
+ {
+ final Map map = objectMapper.readValue(json, new TypeReference