Join Alex Theedom for an in-depth discussion in this video Output the JSON model to a data stream, part of JSON Processing with Java EE.
- [Narrator] Regardless of how the Json object was created, you're going to want to do something with it. Whether that be persisting it to a database, extracting and processing elements of data from it, or simply passing it on to the final consumer. One thing you can do is to output it to a stream so that it can be persisted or shared with another system. In this video, we will discuss ways that you can output the Json object to a stream. At the beginning of this chapter, we saw how to use the Json Reader to load Json data into memory in the form of a Json object. Now we're going to use the Json Writer to flatten this object to a string.
We do this by creating an instance of the Json Writer, by calling a factory method in the Json class. We then parse it either an instance of the Writer object, or an OutputStream object from the Json.io package. Okay, so let's switch to the IDE and see how to do this. Okay, so let's load up the object example six class from the exercise files. In this class we have a method called Build Json Document. This method returns a Json object, and the objective is to write this object to a string. To do this, I need a String Writer instance.
I'm using a String Writer only because it's simpler to test. But you could just as easily parse it an OutputStream instance. So let's create our String Writer. So I create our String Writer, like so. Then we need to create an instance of our Json Writer, and parse it the instance of our String Writer. So, our Json Writer, like so, we create and if you remember, we have a factory method on the Json class that allows us to create a Writer, and we simply parse it our String Writer instance.
As I said before, this could also be an OutputStream instance. Then using the Json Writer instance, we call the Right Object Method and parse it the Json object by calling the Build Json Document method. So, we get our Json Writer, and we call our Right Object Method, and we parse it, our Json object, by calling our Build Json Document. We then need to clean up and close our Writer, and then what we need to do now is to return a string instance.
And we do this by calling the Two String Method on the String Writer. And there we have it, a method that flattens a Json object to a string. And now let's jump to the unit tests. And here all we need to do is to assert that the string output is equal to the Json data that we're using in their example. So, as before, we're going to be using Assert J, and we're going to be parsing it an instance of our Json string, and what we want to do is to assert that it is equal to our Json.
So now let's execute our test and see if it passes. And yes, the test passes. Now what this means is that the Json object has been correctly flattened to a string. And as you can see, it's pretty simple to output our Json object to a string, but equally it could be an output to an OutputStream.
In this course, discover how to use the Java Enterprise Edition (EE) JSON-Processing API. With its two programming models, JSON-Processing is one of the simplest ways to generate, query, and parse JSON data, and you'll find that implementing it in your projects is quick and easy. Join Alex Theedom as he covers an array of topics, including how to use both the object model and the streaming model, how to traverse a JSON structure, and how to parse JSON data using the extensive builder APIs. When you wrap up this course, you'll be ready to develop with this essential and powerful API.
- Working with JSON and Java
- JSON-Processing API features
- The Java EE JSON object model
- Creating a model from a JSON string
- Building a JSON model
- Traversing the JSON model
- Outputting the JSON model to a data stream
- Parsing and writing JSON data