Jackson is a quite common library for dealing with JSON and serialization/deserialization generally in Java and Spring Boot based mostly initiatives.
Jackson handles property names by mapping them as they’re to JSON – so
propertyName
in a POJO can have a correspondingpropertyName
in JSON. This conference is adopted when changing JSON to POJO as effectively, and if the names do not match, the conversion cannot be performed.
Nevertheless, there are various instances during which you’d need the serialized JSON properties to have completely different names, corresponding to standardizing naming conventions for different providers (utilizing instances like snake_case
, as a substitute of CamelCase
) or conflicting property names (a Buyer
can have a firstName
, simply as an Agent
– and a report might include each of their firstName
properties and must be serialized).
Let’s create a Guide
class, with a couple of easy fields:
public class Guide {
non-public String title;
non-public String writer;
non-public int releaseYear;
public Guide() {}
public Guide(String title, String writer, int releaseYear) {
this.title = title;
this.writer = writer;
this.releaseYear = releaseYear;
}
}
Change JSON Discipline Names with Jackson
When often changing an occasion of a Guide
into JSON, we might write the worth of the thing as a JSON string utilizing ObjectMapper
:
ObjectMapper mapper = new ObjectMapper();
Guide ebook = new Guide("Our Mathematical Universe", "Max Tegmark", 2014);
String jsonBook = mapper.writeValueAsString(ebook);
System.out.println(jsonBook);
This ends in:
{"title":"Our Mathematical Universe","writer":"Max Tegmark","releaseYear":2014}
The title
, writer
and releaseYear
are a 1-to-1 mapping to the title
, writer
and releaseYear
fields of the POJO. To alter the identify of a JSON property and retain it after serialization, there isn’t any want to vary your POJO! It is sufficient to annotate the related discipline with @JsonProperty
, and provide the JSON identify:
public class Guide {
@JsonProperty("book_title")
non-public String title;
@JsonProperty("book_author")
non-public String writer;
@JsonProperty("book_release_year")
non-public int releaseYear;
public Guide(){}
public Guide(String title, String writer, int releaseYear) {
this.title = title;
this.writer = writer;
this.releaseYear = releaseYear;
}
}
Now, after we instantiate the thing and convert it to JSON:
ObjectMapper mapper = new ObjectMapper();
Guide ebook = new Guide("Our Mathematical Universe", "Max Tegmark", 2014);
String jsonBook = mapper.writeValueAsString(ebook);
System.out.println(jsonBook);
The code ends in:
{"book_title":"Our Mathematical Universe","book_author":"Max Tegmark","book_release_year":2014}
Change JSON Discipline Names in JSON-to-POJO Conversion?
It is price noting that the identify change is not one-sided. The identical annotation works each methods, and may bridge an incoming JSON with completely different names into a legitimate object. As an example, a JSON representing a ebook with book_title
, would not be mapped to the title
property of the Guide
class by default, as they don’t seem to be the identical.
Since we have annotated title
as book_title
– the conversion works simply superb:
Guide bookReconstructed = mapper.readValue(jsonBook, Guide.class);
System.out.print(bookReconstructed);
This ends in:
Guide{title='Our Mathematical Universe', writer='Max Tegmark', releaseYear=2014}
Observe: To assemble an object from JSON, your class must have an empty constructor as effectively. Jackson first instantiates the empty object, after which populates the fields utilizing the getters and setters.
Annotate Getters and Setters with @JsonProperty
Do you wish to encode completely different names for serialization and deserialization? As an example, you possibly can serialize Guide
right into a JSON with bt
denoting the ebook’s title, however devour JSON with book_title
. You’ll be able to customise the getters and setters of the Guide
class with @JsonProperty
annotations:
@JsonProperty("bt")
public String getBt() {
return title;
}
@JsonProperty("book_title")
public void setTitle(String title) {
this.title = title;
}
This manner, when serialized, the getBt()
technique will serialize the title
as a bt
discipline in JSON. When studying from JSON (deserializing), you will want to provide a book_title
, that will get mapped to title
.
Try our hands-on, sensible information to studying Git, with best-practices, industry-accepted requirements, and included cheat sheet. Cease Googling Git instructions and really study it!
Observe: Each bt
and book_title
will likely be mapped to the title
discipline, however this does not make bt
and book_title
interchangeable. Jackson will not be capable of convert between them with out first changing to title
.
Now, you possibly can instantiate a ebook, serialize it, and deserialize one other String right into a ebook with:
ObjectMapper mapper = new ObjectMapper();
Guide ebook = new Guide("Our Mathematical Universe", "Max Tegmark", 2014);
String jsonBook = mapper.writeValueAsString(ebook);
System.out.println(jsonBook);
String enter = "{"writer":"Max Tegmark","releaseYear":2017,"book_title":"Life 3.0"}";
Guide bookReconstructed = mapper.readValue(enter, Guide.class);
System.out.print(bookReconstructed);
This ends in:
{"writer":"Max Tegmark","releaseYear":2014,"bt":"Our Mathematical Universe"}
Guide{title='Life 3.0', writer='Max Tegmark', releaseYear=2017}
Conclusion
On this brief tutorial, we have taken a have a look at how Jackson maps object fields to JSON, and how one can change the identify of the fields earlier than serialization. We have additionally explored the concept of utilizing completely different JSON names for serialization and deserialization, utilizing the @JsonProperty
annotation on method-level, quite than field-level.