This tutorial shows how to use Jakarta JSON Binding to deserialize JSON data into Java POJOs and to serialize Java POJOs into JSON data. The Jakarta JSON Binding provider used is Apache Johnzon
To get started you will need to clone the jsonb-basic-tutorial project on GitHub. Create a directory on your Desktop or somewhere and execute the following Git command.
git clone https://github.com/tomitribe/jsonb-basic-tutorial.git
Assuming you have Maven installed your next step is to go into the new jsonb-basic-tutorial
directory and execute the Maven install command
mvn install
The jsonb-basic-tutorial uses mock JSON data from Mockaroo.com. The JSON data is stored in a file named MOCK_DATA.json
located in the jsonb-basic-tutorial
directory. It’s a JSON array containing one thousand objects that are structured like the example below.
[{
"id": 1,
"first_name": "Darill",
"last_name": "Kilroy",
"email": "[email protected]",
"gender": "Male",
"ip_address": "68.191.75.95"
}, {
"id": 2,
"first_name": "Enrico",
"last_name": "Yorston",
"email": "[email protected]",
"gender": "Male",
"ip_address": "242.186.201.127"
}]
I created a simple Person
POJO with the correct mappings to enable marshalling between JSON data and Java POJOs. In this case, all I had to do is make sure the Java field names match the JSON field names as shown below and then add Java bean getters and setters.
package example;
public class Person {
// BINDING FIELDS
private int id;
private String first_name;
private String last_name;
private String email;
private String gender;
private String ip_address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
/** and so on for all of the fields */
}
Using Jakarta JSON Binding, you can read JSON data into Java objects easily. All you need to do is create a javax.json.bind.Jsonb
instance using the JohnzonBuilder
and pass it a properly-defined type – in this case we are using a List<Person>
type.
// Read the JSON file into a java.lang.String
String data = new String(Files.readAllBytes(Paths.get("MOCK_DATA.json")));
// You need an instance of the Johnzon Jsonb class to marshal JSON to POJOs.
Jsonb jsonb = new JohnzonBuilder().build();
// Convert a java.lang.String of JSON data into an List of Person type
List persons = jsonb.fromJson(data, new ArrayList() {
}.getClass().getGenericSuperclass());
Serializing the Java POJOs back into JSON data is just as simple.
// Marshal the Person POJOs back into JSON
String myData = jsonb.toJson(persons);
// Create a print writer for the file MY_DATA.json
PrintWriter writer = new PrintWriter("MY_DATA.json", "UTF-8");
// write JSON data to MY_DATA.json
writer.print(myData);
To see the example in action, run the JsonbBasicTutorial
with the following command from a terminal or console (PC users adjust path if necessary).
java -cp target/jsonb-basic-example-1.0-SNAPSHOT.jar example.JsonbBasicTutorial
To check that it worked look at the MY_DATA.json
file under the jsonb-basic-tutorial
directory and you’ll see that it is a syntactic duplicate of the original. This demonstrates that Apache Johnzon successfully deserialized and serialized the JSON data.
Feel free to explore both the limitations and benefits of using Jakarta JSON Binding with Apache Johnzon and have fun!
Here is the full program used to serialize and deserialize the JSON data used in this example.
package example;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.json.bind.Jsonb;
import org.apache.johnzon.jsonb.JohnzonBuilder;
public class JsonbBasicTutorial {
public static void main(String[] args) throws IOException {
PrintWriter writer = null;
try {
// Read the JSON file into a java.lang.String
String data = new String(Files.readAllBytes(Paths.get("MOCK_DATA.json")));
// You need an instance of the Johnzon Jsonb class to marshal JSON to POJOs.
Jsonb jsonb = new JohnzonBuilder().build();
// Convert a java.lang.String of JSON data into an List of Person type
List persons = jsonb.fromJson(data, new ArrayList() {
}.getClass().getGenericSuperclass());
// Marshal the Person POJOs back into JSON
String myData = jsonb.toJson(persons);
// Create a print writer for the file MY_DATA.json
writer = new PrintWriter("MY_DATA.json", "UTF-8");
// write JSON data to MY_DATA.json
writer.print(myData);
} finally {
if (writer != null) {
writer.close();
}
}
}
}