[Android] Where is the best JSON Parser Library?

Tram Ho

[Android] Where is the best JSON Parser library?

Introduction

In the process of developing android applications, when it comes to the efficient and easy-to-use library to handle complex tasks with Network , it is impossible not to mention Retrofit .

But when it comes to libraries to serialize objects into requests or parse JSON responses into objects, we have a few opponents. Today I will introduce and analyze to you what is the best JSON parser in Android.

1. Why was the JSON Parser Library born?

First, before knowing the JSON Parser library, you must have Parse Json in android by creating JSONObject or JSONArray objects. If our json object is quite complex then it becomes very difficult, time consuming, and more code, causing our project to bloat unnecessarily.

Luckily nowadays we have very powerful libraries for automatic conversion from ‘Object’ to ‘json’ and vice versa.

GsonGoogle ‘s own JSON parsing library for Android and is the most popular library (Up to 18.6k stars on Github higher than Jackson – 6.0k and Moshi – 6.6k). It is also the earliest release, with version 1.0 being released in 2008
Jackson – Was a longtime competitor of Gson in an effort to provide the parser faster and more features. Its version 1.0 was released in 2009 codenamed “ Hazelnut ”.
Moshi – The latest and most modern of the 3 JSON Parser Libraries to be released first was in 2015. It’s a kid created by some who have worked on Gson in the past and Square , father Birth of Retrofit

And in my mind a few thoughts popped up.

  • ??? – In many situations, where could one be better than the other?
  • I – Oh! Maybe
  • ??? – We do a few rounds to see which is the best guy?
  • I – That’s right! They will start to find out

2. The war begins

Our contest will have some criteria as follows to evaluate, At first I thought there were 4 criteria. However, I think that the Ease of use criterion is not necessary because none of the 3 is particularly difficult to use, most of which are commonly used.

  1. Featured
  2. Reliability and error handling
  3. Efficiency

2.1 Round-1 FEATURES (Featured)

Gson , Jackson , and Moshi all support full JSON data binding for Java to serialize Java objects to ‘json’ and vice versa. Out of these 3, however, only Jackson supports XML parsing.

All 3 also support custom type adapters, which helps a lot when analyzing more complex API constructs without having to flood your project with unnecessary data classes just for the sake of segregation. Integrate your JSON responses. All 3 also support generic types and are configurable enough for most common use cases.

As for the difference? Gson is the simplest of the 3 but that means it has the least features. Jackson has extensive annotation support.
Some of Jackson’s annotations:

These are annotations that can be used on both classes and properties to modify behavior during serialization or deserialisation. Take @JsonIgnoreProperties as an example. According to the Java documentation:

Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during deserialization)”

I roughly translate:

Annotation can be used to prevent serialization of properties (during serialization) or to bypass handling of read JSON properties (during decoding)”

Jackson has a large number of these annotations that allow a lot of flexibility when it comes to serialization and decoding that Gson does not have or will need more effort to do.

What about Moshi ? Moshi is also not on the feature list. You get meters like @HexColor int which allow multiple JSON representations for a single Java type, and some convenient methods for adapters like JsonAdapter. failOnUnknown() allows you to deny unwanted JSON data.

This is detailed in swankjesse’s comment in a post on Reddit

However, Jackson ‘s XML support as well as its sheer number of Annotations (in fact, they are integrated into easy-to-use Annotations ) put it at the top of the feature list.

Hence 1 point for Jackon this round

2.2 Round-2 RELIABILITY & EROR HANDLING (Reliability and error handling)

Let’s talk about Gson first , because it has a few problems in this area. First of all, it is a pure Java library. Any Kotlin enthusiast will tell you the most important advantage it has over Java is the nullable types , and that allows for safe handling in general and avoiding null pointer exceptions in particular.

What does this mean for Gson ? Since this is a Java library, it instantiates your Kotlin classes regardless of their ability to disable them and thus null values ​​can be deserialized to Kotlin non-null types and cause runtime errors. .

Not only that, but Gson also had other problems such as leaking implementation details of platform types into your encrypted JSON, Date encrypted without timezone so data gets corrupted when encoder and The decoder does not share the time zone. The frustrating part of this error is that it’s also very dangerous to fix: we don’t know what application is expecting the current format, and we don’t know what will go wrong if we change it. (Gson will read RFC 3339 dates like 1970–01–01T00: 00: 00Z by default).

These reasons, among others, are the inspiration behind Moshi’s creation. It was designed to solve the problems Gson encountered. Moshi has some pretty good Kotlin support too, and maybe more.

When it comes to error handling, Moshi also prides itself on predictable exceptions. It will JsonDataException an IOException for IO issues and a JsonDataException for the type mismatch.

As for Jackson , although its core module is purely Java , it has one that supports Kotlin . Jackson also has many unique exception classes that tell you exactly why serialization / deserialisation is not working the way it should. Bealdung’s article explained it perfectly.

But on a deeper look, many of these problems are ones that the library could have better handled to avoid the problem entirely.

For this reason, Moshi wins when it comes to reliability and error handling.

2.3 Final Round PERFORMACE & SIZE (Performance and Size)

Already a lot of benchmaking tests have been performed for these 3 JSON parsers, so we’ll take from these analyzes:

Here’s why you probably comfortable be using the Gson library in 2018

Android JSON Parsers Comparison

Beyond Gson – Evaluating JSON Parsers for Android & Kotlin

The third one on this list is the most recent, with testing done in October 2019. So we’ll look at that first.

Rob Pridham October 2019 performance test:

In the tests conducted by Rob Pridham in October 2019, both during the first run and in the tests that followed, with Jackson being the slowest and Gson not too far away. He said in the article that it’s no surprise that Jackson was the slowest considering the heavily used Annotation .

What’s surprising, though, is how much slower Gson is than Moshi , despite its lack of both features and safety.

Two other tests from 2018 and 2017 seem to give different results.

Danny Damsky August 2018 performance test :

Ilya Eremin ‘s March 2017 performance test:

Both of these tests show that Jackson is the fastest and Moshi or Gson the slowest.

Now let’s find out for ourselves. Why was Moshi the slowest of these two tests, but the fastest because of the difference in the first one?

May 2018 is Moshi ‘s 1.6.0 release date, which adds kapt code generation with Annotation handling. This means you can Annotation your Kotlin data classes so that there are small and fast adapters created for them when compiling them, thus greatly increasing Moshi ‘s performance.

Of course, Ilya Eremin ‘s 2017 test couldn’t use this as it was not released at the time and Danny Damsky, despite using Moshi 1.6.0, didn’t choose this kapt function during testing. by Rob Pridham did.

That explains why Moshi’s test results were the fastest in the most recent test, but how did Jackson suddenly jump down the slowest?

Go on to one more test.

Save my ass: benchmark of json deserializers on Android

Milosz Moczkowski December 2018 performance test :

December 2018 and suddenly, Jackson was once again the slowest and Moshi once again fastest. Okay, how can we try and explain why Jackson is so slow here and in the 2019 test, but the fastest in the old tests?

I can only guess that it is one of two things:

  1. Gson had a huge performance boost towards the end of 2018, thus surpassing Jackson in terms of performance ( Moshi with kapt already)
  2. August 2018 and March 2017 did not use much of Jackson ‘s Annotations

Not to mention, the number of Jackson ‘s methods was much higher than that of Gson or Moshi . Danny Damsky has made this point in his article, so we’ll use that.

Moshi comes with the Okio package as much as twice as many of its methods, but if you’re using other popular Square libraries like OkHttp (probably you), then you’ve got Okio

In both cases, Moshi with kapt wins for both performance and size.

2.4 Result

Gson – 0

Jackson – 1

Moshi – 2

3 Conclusion

Moshi win here.

Gson is quite expected as it was released at the earliest but it is not excellent in any way compared to the other two competitors.

Jackson has many features but is larger and slower than the other two players. Reliability is not equal to Moshi

Is there a situation where Gson and Jackson would be more desirable than Moshi ? As for Gson , if you’re currently using it in a project with no issues and don’t necessarily want to spend time making a move for Moshi , then keep using Gson . It’s Google ‘s child after all, so their team is working to make sure Gson is as solid a library as possible.

You can choose Jackson if you think its variety of Annotations will be useful to you, requiring parsing the data in XML or other formats. If not, Moshi will help you best.

This article I have consulted the documents on the internet Hope everyone will ignore their mistakes and give their suggestions for improvement. I would like to stop writing here! Thank you everyone for watching.

Share the news now

Source : Viblo