From the course: JSON Processing with Java EE

Build a JSON model

From the course: JSON Processing with Java EE

Start my 1-month free trial

Build a JSON model

- Okay, so the first thing you need to do is to open up the ObjectExample3 class file and have a look at the method buildJsonDocument. Within this method we're going to be writing the code necessary to build and construct how JSON objects. Okay, so the first thing we need to do is to create the root elements, which if you remember is a JSON object. And we start by creating a JSON object. And we're gonna be calling it JsonObject, and then from the JSON class we create our object builder. Then we continue by adding the properties and values of the JSON object we want to construct. So first of all, we want to add the title property. I do this by calling the add method, and we pass it a value, key pair. So if you remember, to key name is title, and the value is JSON processing with Java EE. And now we're gonna move on to the next element we wish to add. So now we're going to add the chapters property, now if you remember, the chapter is an array. So we're gonna enter the key chapters, and now what we're going to do is we're going to create an array builder. So again, with our JSON class, we call our createArrayBuilder. And then we now need to start adding our elements. So we call our add method and pass it the first value which is introduction. We continue adding elements like this, the second one is going to be one dot JSON and Java, and so on. And we can continue in this fashion until we have all of our elements added. So the next element I want to add is going to be the second chapter, which is called JSON Processing API Features. We continue like this, adding the elements of our array. Now we have added all of the elements of our array, we don't actually need to call the builder method, although we can, it's not actually necessary and I'm not going to do that because when we call the builder method at the very end, it constructs all of the sub elements that we've created here. So now let's move on to adding our next element, which in this case is going to be our bullion which is our release value. So here, again, we don't have a special add method for bullion, we just simply add the value in like so. We continue, and now we're going to add length, the value here is 60. Now what you'll notice here is that as I add each element, I'm not necessarily specifying the type because this is an overloaded method and it accepts all of the possible types. We continue, and now what we're going to do is we're going to create the source code. And the source code, if you remember, is actually a JSON object, and you've probably guessed what I'm about to do, which is to create a JSON object builder. And just as we did with the array builder, we can start adding our elements. So again, we'll call the add method and we can start adding each of the elements of our source code object. So the first element is repositoryName. And this is a key value pair, and the value is JSON Processing With Java EE. And we continue, we can add the second element which is the URL, and again this is a key value pair. And the value here is github.com/readlearncode. And that's it, in this instance we don't need to call the build method, although again we could if we wanted to. It's not necessary as I mentioned before. The object gets built at the end. And we continue to add the next element which is the complimentary course, this is actually an array of JSON objects. So we add here, the key value which is complimentaryCourse and the value, and again you've probably guessed what I'm going to do here is I'm going to create an array. And just as before we're gonna start adding our array values, now our array values are JSON objects. So, again, you have probably guessed what I'm going to do here which is to create an object builder. And I'm going to start adding elements to our object. So we have here our key value pair, so a title, and the value of our title is Restful Service With JAX-RS 2.0. And that's our first one added. Now lets add a second key value pair, and in this case it's called length, and the value is 120. So lets move on to our next element which again is an object, although it doesn't have to be, it can be any other type of element. And again we're going to add our elements, so again title is our key and the value of our key is Java Enterprise Edition Introduction. Again, this has another value which is going to be our length, just as before. And the length is 130. And there we have it, this is our complimentary course which is an array of JSON objects. So as you can see, every time we need to create either an array or an object, we call the appropriate factory method on the JSON class. And lets have a look at our final element. And this is going to be a null value, so we have a special add null method for this, and it's just gonna be called notes. And this is the very last element, and because it's the very last element we're gonna be calling our build method. And there we have it, we've constructed an entire JSON object, and this is all the code we need in order to construct a JSON object that models JSON cause data. There is one remaining task, and that is to write the unit tests to verify that the object has actually been constructed correctly. And I'm going to set this as a task in your next challenge.

Contents