Skip to content

jakartaee/jsonb-api

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
api
February 24, 2022 14:25
February 16, 2022 14:29
August 12, 2020 14:21
February 18, 2022 14:32
February 28, 2022 17:32
June 28, 2019 15:46
May 25, 2018 16:18

Jakarta JSON Binding (JSON-B)

Maven Central Javadoc Snapshots Gitter License

JSON-B is a standard binding layer for converting Java objects to/from JSON messages. It defines a default mapping algorithm for converting existing Java classes to JSON, while enabling developers to customize the mapping process through the use of Java annotations.

Get it

Maven

<!-- https://mvnrepository.com/artifact/jakarta.json.bind/jakarta.json.bind-api -->
<dependency>
    <groupId>jakarta.json.bind</groupId>
    <artifactId>jakarta.json.bind-api</artifactId>
    <version>2.0.0</version>
</dependency>

Mapping a simple class

Suppose we have the following Java object, which we want to represent with JSON data:

public class User {
  public long id;
  public String name;
  public int age;
}

Using the default mapping, this class can be serialized (as-is) to a JSON string:

Jsonb jsonb = JsonbBuilder.create();

User bob = new User();
bob.id = 1234;
bob.name = "Bob";
bob.age = 42;

String bobJson = jsonb.toJson(bob);
System.out.println(bobJson); // {"id":1234,"name":"Bob","age":42}

Likewise, JSON data can be deserialized back into Java objects:

Jsonb jsonb = JsonbBuilder.create();

String aliceJson = "{\"id\":5678,\"name\":\"Alice\",\"age\":42}";
User alice = jsonb.fromJson(aliceJson, User.class);

How to run the TCK tests

The JSON-B TCK tests are produced as a Maven artifact where the tests use Arquillian + JUnit. To run the TCK tests using your implementation, include the TCK module and apply the appropriate Arquillian container. See the Eclipse Yasson repository for an example of this.

Links