Similar to JAXB for XML processing, the JSR 367: Java API for JSON Binding (JSON-B) defines the binding layer for transforming Java objects to JSON messages and vice-versa, as depicted ahead:
Now let's look at the key components of JSON-B:
Class or Interface |
Description |
javax.json.bind.Jsonb |
This is the core API that provides the functions to serialize a Java object to JSON and deserialize to Java object from a JSON input. |
javax.json.bind.JsonbConfig |
This class provides functions for formatting and encoding to create customized JSON data. |
javax.json.bind.JsonbBuilder |
As the name suggests, this API abstracts the creation of JSON-B instances for the specified configuration or provider. |
javax.json.bind.adapter.JsonbAdapter |
JSON-B supports mapping for a majority of Java data types to JSON. There may be instances when some of the custom Java types may not map to JSON (for example, by default, JSON-B uses ISO date formats required to handle custom date formats. Also, in cases where a client is querying for employee details, we need to respond back with a part of the employee details masking personal details). To handle these situations JsonbAdapter provides the abstraction to implement custom mapping of Java type to JSON and vice-versa. |