Android Data Parser Comparison for Single JSON Object

Aldy Chrissandy
4 min readDec 19, 2019

Why

Sometimes or always when our apps getting bigger we need to store local data on our mobile application. We needs storage of user data in one form or in extreme way store API responses to local storage. This data can be of different formats: Key-value pairs, Application settings, Texts, Spreadsheets, Numbers, Images, Audio or Videos or JSON String. If we have relatively small collection of key-values that we like to save then Shared-Preferences will be enough, but if the case we need to save a lot of data using key-values then it’s gonna be hard to maintain and we need write a lot of code.

I’m lazy programmer so mostly when I need to store a big data to local storage I write the value as JSON String format and save it and when I need it then just read from local storage as String, parse String to JSON and convert it into Object. Now the problem is when our data is gigantic then it takes time to convert JSON String to Object and vice versa, it will take more times until the process is finish.

Lazy to max

HOW

There a bunch awesome JSON parser on but in this project I try to measure and compare the processing time for some JSON parser that currently support Kotlin data class and Retrofit converter and in the end I’m will test library below :

Some side note :

  • I experimented on parsing small JSON file (±5Kb)
  • Only testing single JSON object only, because that’s what I need for now (fell free to Fork my GitHub repository and modify to what you need to test.)
  • Only measure processing time in ms
  • Not measure number of Garbage collector calls while parsing
  • Not measure performance of CPU and Memory of device
  • Not measure how big your APK gonna be when implement the library

Test Methodology

  1. Open App.
  2. Run Parsing Object -> JSONString (for each library)
  3. Run Parsing JSONString -> Object (for each library)
  4. Close Apps
  5. Repeat step 1–3 5 times
  6. Calculate average time

Result

On Emulator Android 8.1 x86 (Multi-Core CPU=4)

Execution time in ms
Execution time in ms

Based on the result looks like “Moshi+MsgPack” is the fastest to convert Object to JsonString and Gson is the fastest when convert JsonString to Object. If I need to choose then it’s better go with Gson, it’s more simple to use and implement. Don’t forget this is only test on single JSON Object only, if we test using more complex JSON format the result will be different for sure.

On Second Thought

After some thinking I’m only need to store one JSON Object, why I need JSON parser to do the process, I can store instance object of Kotlin data class as String by calling .toString(),by using toString() I can save data with value and no need to use third party library to parse the data, but the problem is when I want to convert back to Object.

How if we convert Object to Map<K,V> and store in database and when we need it covert String to Map Object. Now to convert Object to Map we need kotlin-reflect help to fetch variable name on Object, since we only need get variable name of the object so we just need to implement kotlin-reflect-lite for our Android project.

To make it simple lets create extension and generic function for convert Object to Map String and also function to convert MapString to Map

Lets put it on test and we get the result

Execution time in ms
Execution time in ms

The result in here show Kotlin Extension is faster, but in this case I’m running and initialize Jackson parser first then run Kotlin Extension function. If you run Kotlin Extension function first the result will be different.

When using Kotlin-Extension that I created the downside is when we read the cache from local storage and convert it into Map you lose variable type and all variable type become String.

Conclusion

You can decide by yourself which one the best for your project

--

--