Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Get to Know JSON Binding (Part 2)

DZone's Guide to

Get to Know JSON Binding (Part 2)

If you use Java EE and deal with JSON, then you'll want to make sure you're comfortable with JSON Binding. We start with an overview before heading off for customization.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

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.

The 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. 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() methods from the range of overloaded methods 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, but there is plenty more to know about the JSON-B and I cover it in much more detail in my new book Java EE 8: Only What's New.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,java ee 8 ,json-b ,json ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}