RUVIDEO
Поделитесь видео 🙏

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:
►https://futurestud.io/tutorials/gson-getting-started-with-java-json-serialization-deserialization

Watch all of our Gson tutorials in a playlist:
►https://www.youtube.com/playlist?list=PLpUMhvC6l7AOy4UEORSutzFus98n-Es_l

See Gson in action in our Retrofit video series:
►https://www.youtube.com/playlist?list=PLpUMhvC6l7APq7y_FFfK-GEHvcUKqo6SC

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

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

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

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/FutureStudio?sub_confirmation=1

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

Follow us on social media to get updates on new content:
►https://twitter.com/futurestud_io
►https://www.facebook.com/FutureStudioUniversity
►https://plus.google.com/+FutureStudioUniversity

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

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.

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Gson Tutorial — Getting Started with Java-JSON Serialization & Deserialization», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.