DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • Top React Libraries for Data-Driven Dashboard App Development
  • Hiring Full-Stack Developers? 6 Skills To Look For in 2022
  • DocRaptor vs. WeasyPrint: A PDF Export Showdown

Trending

  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • Docker Base Images Demystified: A Practical Guide
  • Java Virtual Threads and Scaling
  • Revolutionizing Financial Monitoring: Building a Team Dashboard With OpenObserve
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Be a Lazy but Productive Android Developer, Part 3: JSON Parsing Library

Be a Lazy but Productive Android Developer, Part 3: JSON Parsing Library

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.

By 
Paresh  Mayani user avatar
Paresh Mayani
·
Apr. 02, 14 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
82.7K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Till the next part, enjoy parsing!

(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.)


file IO Library Android (robot) dev Open source app Gson

Published at DZone with permission of Paresh Mayani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • 4 Email Validation and Verification Techniques for App Developers
  • Top React Libraries for Data-Driven Dashboard App Development
  • Hiring Full-Stack Developers? 6 Skills To Look For in 2022
  • DocRaptor vs. WeasyPrint: A PDF Export Showdown

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!