Moshi: Better JSON parsing for Android!

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. 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?

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

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

Other benefits

Sometimes, it might be the case then 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. If you have an idea about an app or want to make your existing apps better, do let us know about the same!

An app development agency taking small steps towards building a brighter future. Visit us at http://www.terasoltechnologies.com/