Enterprise Java

Get to Know JSON Binding: Overview Series

The Java API for JSON Binding (JSON-B) 1.0 strengthens the Java EE platform’s overall support for the JSON data interchange format. Already, the Java API for JSON Processing (JSON-P) 1.1 has proved popular and together they form the perfect partners that fill a long-standing shortcoming in Java EE’s JSON capacity.

Next article in this series covers the customization of JSON Binding operations.

The JSON-B specification codifies industry practices and methodologies that have become commonplace. It makes heavy use of annotations to mark classes and fields with mapping semantics and provides the extensibility that is so often needed when dealing with complex data structures.

Out of the box, it provides default mappings for serialization and deserialization that meet reasonable expectations. The default customizations are overridable with two customization methodologies:

  1. compile time annotations and
  2. a runtime configuration builder.

For advanced customizations, the API provides adapters and serializers/deserializers for times when the runtime builder and mapping annotations are not sufficient.

Headline Features

The primary feature of this API is the provision of binding support between Java classes and JSON documents in an intuitive and easy to use manner, such that a developer, with no prior knowledge of JSON, should be able to develop effectively with the API. For those with prior experience of other JSON de/serialization libraries such as GSON and Jackson, it will feel very familiar.

The JSON-B API provides two entry point interfaces: Jsonb and JsonbBuilder. The Jsonb interface provides the serialization and deserialization functionality via the methods toJson() and fromJson(), and the JsonbBuilder interface provides the client an access point to a Jsonb instance. It builds the instance based on a set of optional configurations.

Simple Example

Let’s jump in with a simple example that does a round-trip conversion of an instance of the Book.class.

To start a serialization or deserialization you need an instance of Jsonb. You create this by calling the static factory method create() on the JsonBuilder interface. With this instance you can perform all the serialization and deserialization operations you require by selecting the appropriate overloaded toJson() or fromJson() method.

In this code snippet, I call the simplest toJson() method and passed it a book object.

Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom");
String bookJson = JsonbBuilder.create().toJson(book);

The return value of this method is a String that is the JSON data representation of the object passed to the toJson() method.

Now let’s turn our attention to the deserialization operation. It is just as simple as serialization and also requires an instance of Jsonb. In the code snippet, I call the simplest fromJson() method and pass it the JSON String generated by the previous example, this is the JSON data I want to deserialize, and it’s target type as a class type.

String json = "{\"author\":\"Alex Theedom\"," +
               "\"id\":\"SHDUJ-4532\"," +
               "\"title\":\"Fun with Java\"}";
Book book = JsonbBuilder.create().fromJson(json, Book.class);

In these examples, I have used the simplest toJson() and fromJson() method’s from the range of overloaded method available on the Jsonb interface. Now let’s dive a little deeper and look at how to customize the serialization and deserialization process.

Now that wraps up this introduction of the JSON Binding API, there is plenty more to know about the JSON-B.

Published on Java Code Geeks with permission by Alex Theedom, partner at our JCG program. See the original article here: Get to Know JSON Binding: Overview Series

Opinions expressed by Java Code Geeks contributors are their own.

Alex Theedom

Alex Theedom is a Senior Java Developer and has recently played a pivotal role in the architectural design and development of a microservice based, custom built lottery and instant win game platform. Alex has experience of Java web application development in a diverse range of fields including finance, e-learning, lottery and software development. He is the co-author of Professional Java EE Design Patterns and many articles.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button