Join Ketkee Aryamane for an in-depth discussion in this video Need for JSON-B adapters, part of Java EE 8: JSON-B.
- [Instructor] Now that we've seen all the default and the customized settings that you can do for mapping your Java to JSON and JSON to Java, let's look at another topic which is JSON-B Adapters and JSON-B serializers. We'll talk about serializers in the upcoming videos, but let's first understand the need for JSON-B adapters. So now that we've seen so many annotations and then we have seen so many settings on the JSON-B config object that is runtime customization, sometimes all of this is not enough. Whatever kind of custom mapping you are trying to do for your application may not be completely possible with either the compile line, that is annotations, or the runtime, which is the JSON-B config settings.
So in that case, we need something more, and that's why we have a JSON-B adapter. There's also another situation where let's say you're trying to access some third-party libraries. You have certain source code that you do not have an access to. Maybe you're trying to integrate with some other application and it has got their source code, and you do not have an access to it at all. Moreover there are certain classes which don't even have a default constructor at times. So then there in that process of serialization deserialization becomes an issue, and in that case you will have to then write down your own JSON-B adapter to satisfy those very specific requirements that you have.
So, what are JSON-B adapters? They are basically the custom classes that you'll write in order to write out that specific code for serialization or deserialization. So basically, you're going to write a class which implements javax.json.bind.adapter.JsonbAdapter interface. And it gives you a couple of methods to implement: adaptToJson and adaptFromJson. So you have these methods in which you can write your own customization code to serialize and deserialize the objects that you have.
Okay, so we're going to do a small demo for JSON-B adapters. What is the use case, we have the customer bean that we have already seen in all of our previous demos, right? In that, there is one feed which is called isPrimerUser. So if you want to recollect quickly let's go to the customer bean and let's take a look at that feed. Yes, there it is. On line number 20, you have a feed which is called isPrimeUser. And what I want to do is, there's a use case in my application where I want to send only two things or two feeds about the customer.
One, is the customer ID and the other is the isPrimeUser feed. Now I do not have a facility in my annotations way or in my JSON config way in order to send out only specific feeds during serialization. So out of all the feeds that the customer has, I need to send out only the customer ID and the isPrimeUser feed for serialization. And when I get the object back, I want to deserialize it pretty much the same way. So for this requirement, we're going to write a JSON-B adapter class.
So we'll look at the demo in the next video.
- 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