To serialize a Java object to a JSON string, we could use the FasterXML Jackson library.

Configuration
Java Compilation:
Java Runtime:
Maven:
Java Compilation:
Java Runtime:
Gradle:
JDK 11.0.12
JRE HotSpot 11.0.12
Maven 3.8.4
JDK 11.0.12
JRE HotSpot 11.0.12
Gradle 7.4.2
The tutorial is about serialization(convert Java object to JSON), for deserialization(convert JSON to Java object), please visit
To serialize a Java object to a JSON string, we could use the FasterXML Jackson library in Maven: (Using Gradle please see)
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency>
dependencies { implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.9' implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.9' }
For instance, we want to convert the following Java object to JSON String:
public class Square { private int area; private String color; private String texture; // public setter and getter of all the fields }
Create a com.fasterxml.jackson.databind.ObjectMapper object then call writeValueAsString to serialize the object to a JSON string:
public static void main(String args[]) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); Square square = new Square(); square.setArea(100); square.setColor("blue"); String json = objectMapper.writeValueAsString(square); System.out.println("json = " + json); }
Output:
json = {"area":100,"color":"blue","texture":null}
Using a different field name. If you would like field name to be different, use the @JsonProperty annotation:
public class Square { private int area; @JsonProperty("COLOR@!") private String color; private String texture; // public setter and getter of all the fields }
Output will become:
json = {"area":100,"texture":null,"COLOR@!":"blue"}
Customize fields order. If you would like fields of the JSON object in a specific order, use the @JsonPropertyOrder annotation:
@JsonPropertyOrder({ "texture", "color", "area" }) public class Square { private int area; private String color; private String texture; // public setter and getter of all the fields }
Output will become:
json = {"texture":null,"color":"blue","area":100}
Exclude null fields. If you want to exclude any null field to be serialized, use @JsonInclude(JsonInclude.Include.NON_NULL) in the class:
@JsonInclude(JsonInclude.Include.NON_NULL) public class Square { private int area; private String color; private String texture; // public setter and getter of all the fields }
Output will become:
json = {"area":100,"color":"blue"}
Customizing a particular field. If you want to have complete serialization customization of a field, use JsonSerializer:
First create a class extending com.fasterxml.jackson.databind.JsonSerializer with the field data type as the generic type.
e.g. If you want to convert a String field to upper case for all the time, create a class:
public class ColorSerializer extends JsonSerializer<String> { @Override public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(s.toUpperCase()); } }
Using ColorSerializer like:
public class Square { private int area; @JsonSerialize(using = ColorSerializer.class) private String color; private String texture; // public setter and getter of all the fields }
Output will become:
json = {"area":100,"color":"BLUE","texture":null}
Fields are dynamic. If the number of fields is not fixed, instead of defining all the fields, define a map can and use @JsonAnyGetter on the getter method of the map:
public class Square { private Map<String, String> dynamicFields; @JsonAnyGetter public Map<String, String> getDynamicFields() { return dynamicFields; } public void setDynamicFields(Map<String, String> dynamicFields) { this.dynamicFields = dynamicFields; } }
Create the Sqaure with any number of fields as you like:
public static void main(String args[]) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); Square square = new Square(); Map<String, String> map = new HashMap<>(); map.put("color", "blue"); map.put("area", String.valueOf(100)); map.put("icon", "mouse"); square.setDynamicFields(map); String json = objectMapper.writeValueAsString(square); System.out.println("json = " + json); }
Output will become:
json = {"area":"100","color":"blue","icon":"mouse"}