filmov
tv
Gson Tutorial — Getting Started with Java-JSON Serialization & Deserialization
Показать описание
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:
Watch all of our Gson tutorials in a playlist:
See Gson in action in our Retrofit video series:
----------------------------------------
Our book on Gson is also available on leanpub:
----------------------------------------
Checkout 320+ technical in-depth tutorials:
Subscribe for two new videos every week:
----------------------------------------
Follow us on social media to get updates on new content:
----------------------------------------
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!
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.
►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:
Watch all of our Gson tutorials in a playlist:
See Gson in action in our Retrofit video series:
----------------------------------------
Our book on Gson is also available on leanpub:
----------------------------------------
Checkout 320+ technical in-depth tutorials:
Subscribe for two new videos every week:
----------------------------------------
Follow us on social media to get updates on new content:
----------------------------------------
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!
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.
Комментарии