Jackson Custom Serializer and Deserializer Examples

This article shows how to create a Jackson custom serializer and deserializer to parse JSON data that contains a LocalDate type. The Jackson custom serializer or deserializer is useful when we want to process a specific format that is not the default. Table of contents: 1. Setup Jackson 2. Jackson Custom Serializer for LocalDate 3. …

Read more

Jackson and Lombok examples

This article shows how to use Jackson and Lombok together. Table of contents: 1. Download Jackson and Lombok 2. Model with Lombok annotations 3. Parse JSON using Jackson 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 and Lombok 1.18.32 1. Download Jackson and Lombok Declares jackson-databind and lombok at pom.xml. pom.xml <dependency> …

Read more

Write JSON to a file with Jackson

This article shows you write JSON to a file with Jackson. Table of contents: 1. Download Jackson 2. Define a Java Object 3. Write JSON to a File 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Declare jackson-databind in the pom.xml. pom.xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version> </dependency> 2. Define …

Read more

How to ignore a field with Jackson

In Jackson, we can use @JsonIgnore to ignore a single field, @JsonIgnoreProperties to ignore multiple fields and @JsonIgnoreType to ignore a specified type during JSON serialization and deserialization. Table of contents: 1. Setup Jackson 2. Jackson @JsonIgnore – Ignores a field 3. Jackson @JsonIgnoreProperties – Ignores multiple fields 4. Jackson @JsonIgnoreType – Ignores a specified …

Read more

Jackson Java 8 date/time type `java.time.LocalDate` not supported by default

This article shows how Jackson can support the Java 8 date time APIs or JSR-310. Table of contents: 1. Download Jackson 2. Jackson does not support Java 8 date time APIs 3. Make Jackson support Java 8 date time APIs or JSR310 3.1 jackson-datatype-jsr310 3.1 Register JavaTimeModule() 4. Download Source Code 5. References P.S Tested …

Read more

Jersey and Jetty HTTP Server examples

This article shows how to start a Jetty HTTP Sever to run a JAX-RS or Eclipse Jersey application. Tested with Jersey 3.0.2 Jetty 11 Jackson 2.12.2 JUnit 5.4.0 (unit test) JSONassert 1.5.0 (unit test) Maven 3.8.3 Java 11 Tables of contents 1. Using Jersey with Jetty HTTP Server 2. Project Directory 3. Project dependencies 4. …

Read more

Jackson @JsonView examples

In Jackson, we can use the annotation @JsonView to control which fields of the same resource are displayed for different users. Table of contents: 1. Download Jackson 2. Define View Classes 3. Annotate model class with @JsonView 4. Testing the Jackson @JasonView 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Download …

Read more

How to parse JSON string with Jackson

This article uses the Jackson framework to parse JSON strings in Java. Table of contents: 1. Download Jackson 2. Parse JSON String with Jackson 3. Parse JSON Array with Jackson 4. Convert Java Object to JSON String 5. Convert JSON String to Java Object 6. Download Source Code 7. References P.S Tested with Jackson 2.17.0 …

Read more

How to ignore null fields with Jackson

In Jackson, we can use @JsonInclude(JsonInclude.Include.NON_NULL) to ignore the null fields during serialization. Table of contents: 1. Jackson default include null fields 2. Ignore all null fields globally 3. Ignore null fields (Class Level) 4. Ignore null fields (Field Level) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Jackson default include …

Read more

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

Run a Jackson related project and hits the following JsonMerge not found error. Console java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:50) ~[jackson-databind-2.9.0.pr1.jar:2.9.0.pr1] at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:292) ~[jackson-databind-2.9.0.pr1.jar:2.9.0.pr1] at com.hostingcompass.core.utils.PrintUtils.<clinit>(PrintUtils.java:9) ~[main/:na] at com.hostingcompass.app.run.TurtleApp.run(TurtleApp.java:25) ~[main/:na] at com.hostingcompass.app.Main.run(Main.java:42) [main/:na] at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at com.hostingcompass.app.Main.main(Main.java:34) [main/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) …

Read more

Java – Convert Object to Map example

In Java, you can use the Jackson library to convert a Java object into a Map easily. 1. Get Jackson pom.xml <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.3</version> </dependency> 2. Convert Object to Map 2.1 A Jackson 2 example to convert a Student object into a java.util.Map Student.java package com.mkyong.examples; import java.util.List; public class Student { private String …

Read more

How to print an Array in Java

In this article, we will show you a few ways to print a Java Array. Table of contents. 1. JDK 1.5 Arrays.toString 2. Java 8 Stream APIs 3. Java 8 Stream APIs – Collectors.joining 4. Jackson APIs 5. References 1. JDK 1.5 Arrays.toString We can use Arrays.toString to print a simple array and Arrays.deepToString for …

Read more

Jackson 2 – Convert Java Object to / from JSON

In this tutorial, we will show you how to use Jackson 2.x to convert Java objects to / from a JSON. 1. Basic 1.1 Convert a Staff object to from JSON. writeValue(…) – Java Objects to JSON ObjectMapper mapper = new ObjectMapper(); // Java object to JSON file mapper.writeValue(new File("c:\\test\\staff.json"), new Staff()); // Java object …

Read more

Jackson : was expecting double-quote to start field name

A simple example to use Jackson to convert a JSON string to a Map. String json = "{name:\"mkyong\"}"; Map<String,String> map = new HashMap<String,String>(); ObjectMapper mapper = new ObjectMapper(); map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){}); Problem When the program is executed, it hits following error message org.codehaus.jackson.JsonParseException: Unexpected character (‘n’ (code 110)): was expecting double-quote to start …

Read more

Convert JSON string to Map using Jackson

This article uses the Jackson framework to parse JSON strings to a Map in Java. Table of contents: 1. Download Jackson 2. Convert JSON string to Map 3. Convert Map to JSON string 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Simply declare jackson-databind in the pom.xml, and it …

Read more

Jackson Streaming API examples

This article shows how to use Jackson’s Streaming API for both reading from and writing to JSON. Jackson’s Streaming API JsonGenerator – Write JSON data JsonParser – Read and Parse JSON data Table of contents: 1. Download Jackson 2. Write JSON using JsonGenerator 2. Write JSON Array using JsonGenerator 3. Read JSON using JsonParser 4. …

Read more

How to enable pretty print JSON with Jackson

In Jackson, we can use writerWithDefaultPrettyPrinter() to pretty print the JSON strings. Table of contents: 1. Setup Jackson 2. Compact print JSON (Default) 3. Pretty print JSON with Jackson 4. Pretty print JSON with Jackson (Globally) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Setup Jackson Puts jackson-databind at pom.xml. pom.xml …

Read more

Spring 3 MVC and JSON example

In this tutorial, we show you how to output JSON data in Spring MVC framework. Technologies used : Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3 P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath. 1. Project Dependencies Get Jackson and Spring dependencies. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" …

Read more

Jersey and JSON examples (Jackson)

This article shows how to return a JSON response in the Jersey application, using Jackson 2.x. Tested with Jersey 3.0.2 Grizzly 3 HTTP Server Jackson 2.12.2 Java 11 Maven JUnit 5 and JSONassert 1.5 (Unit Test) Table of contents 1. Jackson as the JSON provider in Jersey 2. Project Directory 3. Project dependencies 4. Jersey …

Read more

Illegal to inject a message body into a singleton into public org.codehaus.jackson.jaxrs.JacksonJsonProvider

Problem Using Jackson as JSON provider in RESTEasy. <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>2.2.1.GA</version> </dependency> With RESTEasy auto scanning enabled. <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> When starting up, it hits following errors and failed to start up any of the RESTEasy services. SEVERE: Exception sending context initialized event to listener instance of class …

Read more