Join Ketkee Aryamane for an in-depth discussion in this video JSON-B API overview, part of Java EE 8: JSON-B.
- [Narrator] Let's take a look at few JSON Processing APIs. We are going to talk about certain APIs, and then we'll understand and get into JSON Binding. As a part of Java Enterprise Platform version seven, as of Java Specification Request 353, there was an API added called JSON Processing. Now this is a low-level Java API, which is used to generate, parse, query, and read JSON documents. And it is based on two models. The first one is the object model, and the other one is streaming.
When you talk about the object model, the entire JSON representation is available to you as a single document, and it can be loaded into the memory. Now if it is a huge document, then loading it into the memory is definitely going to leave a bigger memory footprint. However, when you talk about the entire document being loaded into memory, it's also an advantage because you have random access to any element inside the JSON document. When you talk about streaming model, it's of course an event-based model.
So when you actually have a JSON document it doesn't entirely come at the same time. Once, instead, there's one element coming in at one time. So you will not have access to entire document, and if the file is huge, then this is definitely going to act faster, because it does not have to load the entire document into memory at all. So that way, it definitely has a lesser memory footprint. Now, another thing is that when you talk about JSON Processing, the kind of API it is uses JSON Processing types.
We have already seen the JSON structures. We have seen the JSON data types. Now corresponding to each of the data types and structures, you APIs under the JSON-P bundle. So let's say that if you have JSON object, then it is represented by "JsonObject" API. If you want to represent an array in JSON, then there is an API corresponding to that as well - "JsonArray." And then you have "JsonNumber," "JsonString," "JsonValue," "JsonPointer," et cetera. So what I want to say here is that there is no direct binding between the Java object and the JSON representation.
Why do you want direct binding? We all know that there's an architecture which is called "JAXB" - Java API for XML Binding. And that architecture actually allows you to directly bind the Java object's element, that's property, with the XML element. You also have a set of annotations that you can lay out on the field name, or on the methods, getter setters, and it helps you to directly bind that property with a respective XML element. And that is definitely a better way to start converting Java to JSON, or JSON to Java.
Now those of you have worked with XML already know that the object and streaming model is exactly similar to "DOM" model and "StAX" model parsing, respectively, under the XML language. Now to do this direct binding, of course there are third-party libraries, like Jackson, you have Google's Gson, a very famous and popular library. But then, even if there are these third-party libraries, it's still not a standard of the Enterprise platform. So we needed some API which is regarded as standard under the Enterprise umbrella of the Java platform.
And that's exactly why we have JSON Binding. This is what the course is going to talk about. So let's understand the features of JSON Binding. It is a Java Enterprise platform version eight feature, introduced as Java Specification Request 367, and it is the one that is going to provide you a uniform standard for converting Java to JSON and back again. And as we said, it is going to provide you that binding layer, which is missing in JSON Processing API.
So every time you use JSON Processing, you have to deal with those APIs that we talked about. JsonObject, JsonArray, and et cetera. When it comes to JSON Binding, you just have to put those annotations on those fields, and then your Java object is quickly converted to a JSON representation, and vice versa. JSON Binding API is very easy to use, there is no big ceremony here to use JSON Binding API. It's a set of dependencies, very quick to configure in your project. Enter these dependencies into your configuration files of the build tools that you are using, and you already started on that.
Moreover, if you come with a background where you've already used Jackson or Gson, they provide those binding layers already. So you will possibly already know how to use JSON Binding. JSON Binding API works perfectly with JSON Processing API. In fact, JSON-B internally works with JSON-P, which is that low-level Java API we've talked about in the previous slide. It works on both of those model and streaming APIs, and when you talk about JSON Binding API, it will use JSON Processing API internally to do low-level serializations and deserializations.
So when it comes to JSON Binding API, it gives you a lot of mappings to do. First of all it has got a long list of default mappings that it can do when it tries to convert a Java to JSON or JSON to Java. Not only that, if you're not satisfied with the default settings, yes, there's a host of customizations that you can run, and ask JSON Binding to do the customized level of mapping, for conversion of your Java objects to JSON, and JSON to Java. All the APIs of JSON Binding reside in the "javax.json.bind" package.
Okay, so now we've talked a lot about the JSON structures, the JSON types, the JSON APIs [uintelligible], the famous and the popular ones, and then why we have JSON Binding API, and the features of JSON Binding API. Up next, we'll see the setup for how do you make JSON Binding project, and then let's get started on with the demos.
- JSON and JSON-B API basics
- Default mapping
- Defining beans and data
- Customized mapping
- Customizing the order of serialized properties
- Customizing constructors
- Working with binary data
- Custom serializers and deserializers
- Demo: Using JSON-B in a web application