Simple JSON serialization

As part of Jakarta EE there is the JSON-B library, which also can be used standalone, it allows you to easily serialize Java objects to JSON and deserialize from JSON to Java objeects.

The shortest quick and dirty serialization example snippet would be:


  Jsonb jsonb = JsonBuilder.create();
  String json = jsonb.toJson(myObject);
        

The shortest quick and dirty deserialization example snippet would be:


  Jsonb jsonb = JsonBuilder.create();
  MyObject myObject = jsonb.fromJson(json, MyObject.class);    
        

To get the JARs you can add the following maven dependencies to your project:


  <dependency>
    <groupId>javax.json.bind</groupId>
    <artifactId>javax.json.bind-api</artifactId>
    <version>1.0</version>
  </dependency>              
  <dependency>
    <groupId>org.eclipse</groupId>
    <artifactId>yasson</artifactId>
    <version>1.0</version>
  </dependency>
  <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1</version>
  </dependency>
        

As you can see it is really simple to use JSON-B.

To get more depth about JSON-B go to its website

Posted March 17th, 2018

Up