JSON parsing in Android using LoganSquare

You can clone this project from this Github repository.

What’s the motivation?

As I was working on my app update (Earthquake App), I realized that the native JSON package available in android sdk is just not good enough in terms of performance and memory consumption. So, I started looking for some other option and very soon I found myself in a hard place deciding between GSON and Jackson (most popular JSON parsing libraries in java).

As my use of JSON parsing is very performance centric, I kept on searching until I stumbled upon LoganSquare (a relatively new library) which claims to be the fastest JSON parsing and serializing library available for Android outperforming GSON and Jackson’s Databind library by 400% or more.

Being a beginner, I never used any Model based object building practices and that’s why I was looking for some working tutorial project or some article at least which can help me get started. As I couldn’t find any of those, I had to get my hands dirty but nonetheless I made it through. Also, I decided to write one myself for beginners like me who wants to use it.

Why use LoganSquare instead of GSON or Jackson?

Although, I’ve found a few stackoverflow answers supporting LoganSquare’s claim, this article is not about which one is better than other but a mere article to help those beginners who’ve decided to use it. And do remember that it’s totally up to you to use whichever you see fit for your usecase.

A picture is worth a thousand words so here is the benchmark image they (LoganSquare) have on their Github page.

Let’s get to work!

For the sake of keeping this article as short as possible, I’m assuming that you know how to setup your android project (Google it if you don’t) and that’s why I’m not putting unnecessary steps for project setup.

The first step of using any library is to import it in your project and for doing so just head to your app build.gradle file and add the following lines into their respective places.

buildscript {
repositories {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
apt 'com.bluelinelabs:logansquare-compiler:1.3.6'
compile 'com.bluelinelabs:logansquare:1.3.6'

After this your gradle file should looks like this. If yes, then hit the Sync Now button on top right corner.


For example JSON file I’m using this usgs.gov file which has data about recent earthquakes. The structure of this file is like this (on left).

This complete file is a JSON object in which metadata is another JSON object having info about this file, features is a JSON array which has array of earthquakes and bbox which won’t be of any use here.


Now inside“featuresevery JSON object has two JSON objects namely “properties” (has data about earthquake) and “geometry” (has data about it’s location) plus a string “id”. For this example project I’ll be using only a few data from properties.

Let’s start building our model class. For those who don’t know what a model class is let me give you a quick brief that a model class in Java is basically used to create an object of a particular structure (or model). In our case it’s used to parse JSON object(s).

Go to your project package and make a new class QuakeModel (name can be anything). Inside this model class you’ll have to define variables of same type and name. By default, LoganSquare assumes that the JSON field name will match your Java variable’s name unless the name parameter has been used in the field’s “@JsonField” annotation. I am using the same names for all my variables except one. If you wanna change the names of your variable you’ll have to declare name parameter in it’s JsonField annotation. My custom name variable is id and it looks like this.

public String quake_id;

LoganSquare believe in annotations (a lot) so it require every variable and class in your model should be annotated. Every variable should be annotated with “@JsonField” and every class (even the QuakeModel class itself) should be annotated by @JsonObject”. If you want LoganSquare to detect all non-private fields which aren’t annotated with @JsonField, you’ll have to set the fieldDetectionPolicy of the @JsonObject like this below:

@JsonObject(fieldDetectionPolicy = FieldDetectionPolicy.NONPRIVATE_FIELDS)
public class QuakeModel {

Note that LoganSquare can parse public and default variables without issue but for private variables you’ll have to declare getter and setter methods like this below:

private int mag;

public int getMag() {
return mag;

public void setMag(int i) {
mag = i;

Should you be needing it, there are optional callback methods available which will be called before serialization and after parsing. They are -

    @OnPreJsonSerialize void onPreSerialize() {
// Do some pre-processing stuff before serializing here
@OnJsonParseComplete void onParseComplete() {
// Do some post-processing stuff after parsing here

The structure of your model class should reflect that of parsing and serializing JSON file. So for our QuakeModel your class should look like this

One thing to be noted here is that I’m gonna access these variables directly which might not be a very good approach. So, if you want to then you can define methods to access these variables.

Now go to Main class and start fetching and parsing JSON. Do remember that you need INTERNET permission and fetching quakes on UI thread is prohibited. I’m using AsyncTask doInBackground() method for quakes fetching and parsing then inside onPostExecute() I am inflating the UI with this data. My Main class looks like this:

This approach of parsing data from JSON file is a bit different from that of JSON package in android which has getJSONObject(), getString(), getInt() etc. like methods but it’s surely very memory and performance efficient.

Another thing to be noted here is that I used native HttpURLConnection for fetching JSON file which is not a good choice. Perhaps, you should be using some better approach like using Google’s Volley or OkHttp.

To actually see the performance of LoganSquare, you can use this usgs.gov file which has data of thousands of earthquakes.

To see this project in action, clone this Github repository and build the app.