This is a series of JSON tutorials in simple step-by-step way, explaining what-why-how of JSON + supporting libraries.
JSON (JavaScript Object Notation) is one of the most popular data-exchange format, and provides an alternative for another popular XML data-exchange format. It’s syntax is derived from Javascript objects structure [the name itself makes it evident]. Being easy to understand, read and write, it quickly became the first choice for data-interchange between desperate systems.
This series of JSON tutorial goes over the JSON basics and the popular Java libraries available to process JSON data, mainly the most popular ones including Jackson & Google Gson. Let’s begin.
JSON Hands-on Examples
In this tutorial series, we will go through JSON structure, common usage of JSON, using the popular libraries available.
JSON Structure
Json message structure and usage.
Popular JSON libraries
1. Jackson JSON Processor:
Jackson JSON library seems to be the first choice for java based applications in industry today. It comes bundled with built-in JSON parsers, binding tools used to map POJO’s to JSON and many more.
Jackson provides following different ways to process JSON:
- Data binding : Converts JSON to/from POJOs using getters or annotations.
- Tree Model : Similar to DOM tree, represents an in-memory tree representation of a JSON document.
- Streaming API : Reads & Writes JSON sequentially(Incremental processing). This is by far the most performant way to process JSON.
Jackson Object to JSON Data binding Example
Jackson Map to JSON Data binding Example
Jackson JSON annotations Example
Complete examples for commonly used Jackson annotations including @JsonProperty , @JsonIgnoreProperties , @JsonAnySetter, @JsonAnyGetter & @JsonSerialize.
2. Google GSON library:
Gson library provides somewhat similar set of functionality as (and actually competes with) Jackson library. Gson library was originally developed for internal projects in Google, but then made open to public use.
Compare to Jackson, Gson too provides following different ways to process JSON:
- Data binding : Converts JSON to/from POJOs.
- Tree Model : Similar to DOM tree, represents an in-memory tree representation of a JSON document.
- Streaming API : Reads & Writes JSON sequentially(token by token).
Gson Object to JSON Data binding Example
Gson Custom Serialization Example
Customize the way the serialization/deserialization should happen.
Gson Exclusion Strategy Example
Exclude/skip certain field(s) from serialization/deserialization.
Gson Annotations Example
Complete examples for commonly used Gson annotations including @Expose , @SerializedName, @Since, @Until & @JsonAdapter.Post also discusses about Gson TypeAdapter usage.