Gson Tutorial — Getting Started with Java-JSON Serialization & Deserialization

Published: 02 June 2017
on channel: Future Studio
102,692
1.4k

In this video you'll get started with Gson. Gson is a library to (de)serialize JSON from and to Java objects:
►Introduction: [0:07]
►Gson Setup: [1:15]
►JSON Basics: [1:57]
►Serialization (from Java object to JSON): [4:21]
►Deserialization (from JSON to Java object): [6:52]
►Summary: [8:54]

Tip: turn on subtitles to deal with my accent. A shortened transcript is also available below.

Find the tutorial for an easy read here:
►https://futurestud.io/tutorials/gson-...

Watch all of our Gson tutorials in a playlist:
►   • Gson Tutorial Series  

See Gson in action in our Retrofit video series:
►   • Android Retrofit Tutorial Series  

----------------------------------------

Our book on Gson is also available on leanpub:
►https://leanpub.com/gson-enjoy-json-d...

----------------------------------------

Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.

Checkout 320+ technical in-depth tutorials:
►https://futurestud.io

Subscribe for two new videos every week:
►https://www.youtube.com/c/FutureStudi...

----------------------------------------

Follow us on social media to get updates on new content:
►  / futurestud_io  
►  / futurestudiouniversity  
►https://plus.google.com/+FutureStudio...

----------------------------------------

Shortened Transcript:

Gson is a powerful library to map automatically from Java objects to JSON and from JSON to Java objects. With a single line of code you can these transformations without worrying about the exact details or changes in the data models. Gson does everything for you.
Gson works either on Android or any Java project. The example project we're going to work in will always be in Java, but it doesn't mean you can't use on Android. It works on both platforms the same way.
Here we have our Java example project. I've also already added the gradle dependency here. If you want to use Gson in your project you just simply add this line to your gradle build.
The UserSimple class has four properties. It has two strings: the name and the email. It has an integer value: the age, so in sense of 27, and it has a boolean value isDeveloper, which is true or false. For everyone who didn't work with JSON before, let's create a quick JSON object, which is appropriate for this class. JSON objects are alway indicated by the curly brackets {}. This would be an appropriate JSON representation of one of these objects.
When we want to transform from a Java object, we need to have that Java objects. Let's use the same value we've just used before: Norman, my email, 27, and true.
How do we create a JSON out of this [Java] object? We need a Gson instance. What we can now call on this Gson instance is toJson(). The parameter for the toJson() is simply the object we've just created. If you make this to a String we've this JSON object here. Actually it's a String object, which contains a JSON.
We have a JSON result. As you can see we've added whitespaces and line breaks to make it look a little nicer, so we humans can read it. Gson actually, by default, makes it as small as possible. So no whitespaces, no line breaks. If you format this the same way, you'll see it's the exact same result. The values are the same, the property keys are the same, everything is identical. That with just one line of code, fantastic!
Let's do the other direction, when we have a JSON and want to make Java objects out of it. This process is called deserialization. The process is fairly similar. We take a Gson instance and on the Gson instance, we call fromJson(). We can pass a string so we'll just pass the userJson string. Here we have a second parameter. As you can see when you look at the JSON objects they're as minimal as possible by just stating the values. You need to know what kind of object is incoming. Here we would say this is of the type UserSimple.class. Now Gson can tell, you have this incoming JSON and we're going to map it to this UserSimple class and can set the properties correctly. Let's make a user object out of and run the same thing.
What we would expect that the email, the [is]Developer, the name are all the same, but the age is 26 now. As you can see everything worked, we've the name correctly, we've the email correctly, the age is 26 because the JSON says 26, and we have the isDeveloper, which is true.
What you've seen in this short example is how you can use Gson to transform automatically from JSON to Java objects and the other way around. Obviously, there is a lot more to explore. In the next video, we'll look at how you can map nested objects with Gson.


Watch video Gson Tutorial — Getting Started with Java-JSON Serialization & Deserialization online without registration, duration hours minute second in high quality. This video was added by user Future Studio 02 June 2017, don't forget to share it with your friends and acquaintances, it has been viewed on our site 102,692 once and liked it 1.4 thousand people.