Terasol's Insights to Innovative Software Development Services

Exploring Moshi with JSON: A Better JSON Parsing For Android!

Written by Anoop | May 2, 2019 6:30:00 PM

With JSON being a popular data interchange format, no wonder why there are tons of efforts from the dev. community to make its processing even faster. Even more so for Android development. While you can manually parse it quite easily, there are certain libraries that do it efficiently and quickly. One in specific- Moshi.

Why a library?

While JSON parsing is possible quite easily with Java's built-in support, it can still be quite a tedious task to parse it efficiently. It even gets harder to manage each possible case of data values when the size of the data set increases.
For instance, developers at Google knew that with it being a popular format, all developers are doing the same repetitive tasks of parsing with varying degrees of efficiency.

So to iron out such issues, they developed GSON. Which is basically Google's take on JSON-related operations, and makes it much easier and faster to work with it in Android.

It is a quite widely appreciated library throughout the Android Developer Community. But now we have a new contender in the arena - The Incredible . . . . .

.

.

Moshi !

Exploring Moshi with JSON

Hint: You are already using some of Moshi! Yes, even when you haven't included its library.

Alright, to the code now.
Consider this sample snippet:

{

"id" : "m1",

"movie_name" : "Iron Man",

"rating" : 7.9

}

If you do manual parsing, you will have to make a data class to hold this data. This data class might somewhat look like this:

class MovieItem{

String id;

String name;

float rating;}

Data class for JSON object

As we can see, there is a 1:1 logical relation between the field of the class and the JSON format object's properties. While manually parsing, we will have to write the code that will use JsonObject's getString() and similar methods to fetch values.

With Moshi, all you need to do is provide annotations in the above class and it automatically maps it to the received object without the use of getString() or other silly methods.

Code? Here we go:

class MovieItem{

@Json(name="id") String id;

@Json(name="movie_name") String name;

@Json(name="rating") float rating;

}

Annotated data class for Moshi

And that's it. Moshi will automatically map the properties of this object to fields of your data class. See? You already applied some part of Moshi.

The string values provided in the @Json annotation are used to map it to the object's property having that name.

If the name of the data class's members already matches the name of a property in an object, you don't even need to use the annotation at all! This means the above data class can be minimized to :

class MovieItem{

String id;
@Json(name="movie_name") String name;
float rating;

}

Minimized data class

Note how annotations of id and rating are no longer required but are required for name. That's because the object has a property named "movie_name" while we want our variable name to be just "name".

Since the property name is different from the member variable's name, the name of the required property needs to be provided in the annotation for that variable explicitly.

Direct benefits

If you use Kotlin, which by the way you should if you aren't already, you'll be happy to know that Moshi comes with Kotlin-adware extensions, which makes it even easier to integrate with your existing Kotlin code.

One advantage that it has over GSON is that it's built on top of Okio, because of which it has which it can share buffer segments with Okhttp if you are using it already!

Other benefits

We saw how Moshi helps in de-serialization (conversion of JSON string to a byte sequence, or simply, a Java object) of JSON in Android. Similarly, it can help for serialization of the object(s) also i.e. convert an object or list of objects to a JSON Object or JSON Array respectively.

Sometimes, it might be the case that instead of 1:1 mapping, a sequence of steps for conversion of JSON to java object(s). There is also support for this operation too. For that, you need to define an adapter that will have annotated methods to dictate the rules of conversion of from or to JSON and pass it to Moshi's builder.

This and many more features of Moshi can be studied on its official Github page.

We aim to improvise our approach of delivering quality apps that are also performant. With this in mind, our developers keep looking for new tools that can not only increase the development speed but which also contribute to the quality of our apps.

Are you in a search of a mobile app development company?

Do you have an app idea that can create marvels? You would need the assistance of an app development company and your search comes to an end with Terasol Technologies. 

If you have an idea about an app or want to make your existing apps better, do let us know about the same!