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?
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 more hard to manage each possible cases of data values when the size of 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 varied degree 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:
Data class for JSON object
As we can see, there is a 1:1 logical relation between the field of class and the JSON 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:
@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 is used to map it to the object’s property having that name. If the name of data class’s members already matches the name of a property in object, you don’t even need to use the annotation at all! Which means the above data class can be minimized to :
@Json(name=”movie_name”) String name;
Minimized data class
Note how annotations of id and rating is no longer required, but is required for name. That’s because the object has 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.
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-aware extensions, that makes it even more easy to integrate with your existing Kotlin code.
One advantage that it has over GSON is that its built on top of Okio, because of which it has which it can share buffer segments with Okhttp if you are using it already!
We saw how Moshi helps in de-serialization (conversion of JSON string to byte sequence, or simply, a Java object) of JSON in Android. Similarly, it can help for serialization of 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 the 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 which 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 at its official Github page.
We aim to improvise our approach of delivering quality apps that are also performant. With this in in mind, our developers keep looking for new tools that can not only increase the development speed, but which also contributes 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!