Be a lazy but a productive android developer – 3 – JSON Parsing library

By -

Welcome to the part 3 of “Be a lazy but a productive android developer” series. If you are lazy android developers for JSON parsing but want to be a productive by using JSON parsing library then this article is for you.

This series so far:

  • Part 1: We looked at RoboGuice, a dependency injection library by which we can reduce the boiler plate code, save time and there by achieve productivity during Android app development.
  • Part 2: We saw and explored about Genymotion, which is a rocket speed emulator and super-fast emulator as compared to native emulator. And we can use Genymotion while developing apps and can quickly test apps and there by can achieve productivity.

In this part

In this part, we are going to explore some JSON parsing libraries which are existed in market and we can use either of them into our app development to improve App performance and can achieve productivity at the same time.

JSON Parsing:

Previously I have already written an article on “JSON Parsing” using the classes of org.json package, if you don’t know about JSON Parsing or have missed reading that article then here it is: JSON Parsing in Android.
Now, Instead of using native package (org.json) and its classes, if we use some JSON Parsing libraries exist on web then we could achieve app performance, 2 libraries are listed below which are widely used by android developers:

  1. GSON
  2. Jackson

“Why should we use such JSON libraries if there is already org.json package and its classes available?”

Before talking and exploring about these libraries, let’s have a deep look into the org.json package. When there is a requirement of XML parsing, there are 2 general strategies:

  1. DOM
  2. SAX

DOM (Document object Model) loads the entire response/data into the memory and allows developer to query the data as they wish.

SAX (Simple API for XML) parses the node to node and uses top-down traversing. And the main thing is that it performs parsing without storing XML and presents data to user as a stream. And the second strong point is that it parses fast as compared to DOM and prevents less memory as compared to DOM.

Now, when there is a requirement of JSON Parsing, usually JSONObject and JSONArray classes get strike in our mind and obviously its default choice for the JSON parsing because it’s simple, easy to use and is available since the very beginning (API Level 1).

But JSONObject and JSONArray classes follows the DOM parsing technique, and so it requires you to load entire JSON data/response into a string before you can parse it, and hence it’s main and biggest weakness of it.
This may not be a good idea and may be inefficient when we have large JSON response/document to be parsed.

1. GSON

To provide an alternative and overcome such issues, Google has already given JSONReader which presents data as a Stream same like SAX but this class is available in API level 11 and upper version so if you would want to provide compatibility to lower versions, it doesn’t have any such functionality.

But you can provide compatibility to lower versions by using GSON library as Google has already made it open source and made JAR file available.

Over and all, android.util.JsonReader and com.google.gson.stream.JSONReader both are the same code. But the main advantage of using GSON as a standalone library is, it always has the latest stuffs and improvements.

What does GSON do?
It’s actually a standalone and open source library which is used to convert JSON data into Java objects and vice versa. In simpler words, it can be used for parsing and building JSON. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. It simply provides toJson() and fromJson() methods to convert Java objects to JSON and vice-versa.

You can read more about it and download GSON library from here: https://code.google.com/p/google-gson/

For example:

[
    {
        "name": "Paresh",
        "address": "ahmedabad"
    },
    {
        "name": "Hiren",
        "address": "America"
    }
]

Gson gson = new Gson();
Type collectionType = new TypeToken<List<PersonBean>>(){}.getType();
List<PersonBean> details = gson.fromJson(strJsonData, collectionType);

2. Jackson

It’s again a multipurpose and open source Java library for processing JSON data format. As on their website, Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.

You can download JackSon library from here: http://wiki.fasterxml.com/JacksonDownload and read documentation here: http://wiki.fasterxml.com/JacksonDocumentation.

Which library I should use and best, GSON or Jackson or any?

It’s upto you how you consider particular library is the best, whether it’s in terms of achieving performance, code optimization or optimizing UI or something else.

In terms of reduction of lines of code, both the libraries are good, I mean you can reduce boilerplate code using either of these library.

In terms of performance, Jackson gives better performance and does quick parsing, one gentleman has done comparison of JSON Parsers, do check it!

android json parsing comparison

android json parsing comparison

Hope you liked this part of “Lazy android developers: Be productive” in which we talked about JSON parsing libraries and benefits of using either of them. Hope you will use either of any and it could help you to optimize your productivity.

(P.S. Personally I haven’t used Jackson in any of my project but will be exploring it sometimes later and will write an article on it with possible example code.)

See you in the next episode, where we shall cover Part 4: Card UI in Android. Till than try out each libraries for at least once.

CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development

Loading Facebook Comments ...
Loading Disqus Comments ...