Jackson is a Java API that allows you to convert objects to JSON and vice versa.
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>defines the order of the fields in the JSON
@JsonPropertyOrder({"id", "name", "age"})
public class Person {
private int id;
private String name;
private int age;
}defines which fields will be ignored when serializing/deserializing
@JsonIgnoreProperties({"id", "name", "randomValue"})
public class Person {
private int id; // will be ignored
private String name; // will be ignored
private int age;
}{
"age": 20,
"randomValue": "random"
// will be ignored as well
}defines the name of the field in the JSON
public class Person {
@JsonProperty("person_id")
private int id;
}{
"person_id": 1
}defines a method that will be used to get the map of additional properties, that are not defined in the class
public class Person {
private Map<String, String> additionalProperties = new HashMap<>();
@JsonAnyGetter
public Map<String, String> getAdditionalProperties() {
return additionalProperties;
}
}defines a method that will be used to set the additional properties, that are not defined in the class
public class Person {
private Map<String, String> additionalProperties = new HashMap<>();
@JsonAnySetter
public void setAdditionalProperties(String key, String value) {
additionalProperties.put(key, value);
}
}defines a custom serializer for the field
public class Person {
@JsonSerialize(using = CustomSerializer.class)
private String name;
}CustomSerializer.java
public class CustomSerializer<T> extends StdSerializer<T> {
public CustomSerializer() {
this(T.class);
}
@Override
public void serialize(
T value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeNumberField("someValueInsideTObject", t.getSomeValueInsideTObject());
jgen.writeEndObject();
}
}defines a custom deserializer for the field
public class Person {
@JsonDeserialize(using = CustomDeserializer.class)
private String name;
}CustomDeserializer.java
public class CustomDeserializer extends StdDeserializer<String> {
public CustomDeserializer() {
this(String.class);
}
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
String text = jsonParser.getText(); // text is the value of the field
return text.toUpperCase();
}
}defines the format of the date field
public class Person {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate birthDate;
}{
"birthDate": "2022-01-01"
}to use the @JsonFormat annotation, you need to register the JavaTimeModule module
static {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
}defines an alias for the field
public class Person {
@JsonAlias({"person_id", "id"})
private int id;
}{
"person_id": 1
}or
{
"id": 1
}defines that the field will be ignored when serializing/deserializing
public class Person {
@JsonIgnore
private int id;
private String name;
}{
"name": "John"
}defines that the field will be unwrapped when serializing/deserializing
public class Person {
@JsonUnwrapped
private Address address;
}{
"street": "Main Street",
"city": "New York"
}defines that the fields will be serialized/deserialized as a reference
managedreference is serialized as a valuebackreference is ignored
public class Person {
@JsonManagedReference
private Address address;
private String name;
}
public class Address {
private String street;
private String city;
@JsonBackReference
private Person person;
}Person.json
{
"name": "John",
"address": {
"street": "Main Street",
"city": "New York"
}
}Address.json
{
"street": "Main Street",
"city": "New York"
}defines the root name of the JSON
@JsonRootName("person")
public class Person {
private int id;
private String name;
}{
"person": {
"id": 1,
"name": "John"
}
}used to convert objects to JSON and vice versa
use the writeValueAsString method to serialize an object to JSON
public class Serializer {
public <T> String serialize(T object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(T);
}
}use the readValue method to deserialize JSON from a file to an object
public class Deserializer {
public <T> T deserialize(String path) throws IOException {
ObjectMapper mapper = new ObjectMapper();
T object = mapper.readValue(new File(path), T.class);
return object;
}
}use the readerFor().readValue method to deserialize JSON from a string to an object
public class Deserializer {
public <T> T deserialize(String json) throws IOException {
ObjectMapper mapper = new ObjectMapper();
T object = mapper.readerFor(T.class).readValue(json);
return object;
}
}