org.json.JSONObject vs Gson library JsonObject - android

What are differences between this two classes?
If someone uses Gson library is it preferable to use com.google.json.JsonObject over org.json.JSONObject?
Could anybody list pros and cons of these 2 choices?

Following are the main differences:
1) GSON can use the Object definition to directly create an object of the desired type. JSONObject needs to be parsed manually.
2) org.json is a simple, tree-style API. It's biggest weakness is that it requires you to load the entire JSON document into a string before you can parse it. For large JSON documents this may be inefficient.
3) By far the biggest weakness of the org.json implementation is JSONException. It's just not convenient to have to place a try/catch block around all of your JSON stuff.
4) Gson is the best API for JSON parsing on Android. It has a very small binary size (under 200 KiB), does fast databinding, and has a simple easy-to-use API.
5) GSON and Jackson are the most popular solutions for managing JSON data in the java world.

Many JSON implementations are available in the market and most of them are open source. Each one has specific advantages and disadvantages.
Google GSON
Jackson
org.json etc.
Google GSON click for official documents
Provide simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
Allow pre-existing unmodifiable objects to be converted to and from JSON
Extensive support of Java Generics
Allow custom representations for objects
Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
Jackson click for official documents
Streaming API or incremental parsing/generation: reads and writes JSON content as discrete events
Tree model: provides a mutable in-memory tree representation of a JSON document
Data binding: converts JSON to and from POJO’s
Some comparison blogs click here blogs1, blog2
I personally done a benchmark for serialization and deserialization using GSON vs Jackson vs Simple JSON
Very small object: Google gson performs faster than Jackson and Simple JSON
Large objects : Google gson performs faster than Jackson and Simple JSON

I did experiment with 456 Kb json file on real Pixel 3 device Android 11.
I need to create a backup file from database. Model has 3 objects with one-to-many relationships: Note, Item, Alarm.
Note -> fields and List<Item>.
Item -> fields and List<Alarm>.
Alarm -> fields
Serialization result:
gson -> 75ms; org.json -> 63ms
gson -> 83ms; org.json -> 67ms
gson -> 73ms; org.json -> 62ms
As you can see the default android org.json is faster than GSON.
If you have a time to create a mapping for your model, I recommend using the default org.json.
If you want to create json faster than gson, but more easy than org.json try using moshi or maybe kotlinx-serialization.

Related

Is there something similar to OPENJSON in android?

OPENJSON is a table-valued function that parses JSON text and returns objects and properties from the JSON input as rows and columns.
OPENJSON
I want to deserilize Json into objects, but I want to access parts of the Json not parse it all.
I'm using android Kotlin with Room database, if there is a solution in Room maybe.
You can use any pure java library in android. Google GSON would be a good choice and with some minimal tweaking you can ignore unnecessary parts of JSON objects

Should I use json objects directly?

I have always used a fromJson method to convert my json object to my model object. So let's say that my JSON has a car field which has my car models data, so I always parse the Json. Now I found out that it's also a common practice to use the json objects directly in the application. That has gotten me thinking
should I parse the json back to my models, if yes then why and if no then why not?
It depends but in general - yes, you should have transformation logic to convert your jsons (essentially, DTOs) into your entities (models as you call them). Here is my reasoning:
Your entities will likely be different from corresponding DTOs. As an example, your json model can have date/time information as long UTC msecs, but your entity model will likely be more usable if it has localized Date's
If you're exposing your entities (for example, as part of a library), you'll have much more flexibility in making changes to your remote apis (if such exist of course) without breaking your library's consumers codes
It completely depends on your project structure, if you have POJO classes defined in the models then converting them is the way to go. It makes your code more readable and fulfils your model which makes fetching the data easier. If you use a JSONObject directly then pulling data from it requires a bit more code. In terms of performance they both should have equal impacts since both are 'Objects' and will consume resources equally.
I vote for parsing them back to Models because of the simplicity it
enables for using those values further in your coding.

Is Default JSON LIB robust enough for real time twitter feed?

I am going to be consuming real time twitter feed and parsing it into objects for list view display. I need robust solution is default json lib good enough for this task or do I need to use Gson / jackson lib?
Check Gson and JackSon:
GSON
JACKSON
EDIT: I did not see you mention this libraries. Yes, you should use them. This are robust
libraries, I used them to consume Json webservices and no problem so far.
GSON/Jackson are just the libraries that allow you to parse json responses into objects or the other way round. However, the default implementation of json provided in android framework is capable enough of dealing any response string. The only drawback or turn-off is that you need to write extra code for simple things to get done.
Following are some links with bench marking of JSON libs...
http://blog.novoj.net/2012/02/05/json-java-parsers-generators-microbenchmark/
http://danielywoo.blogspot.in/2011/04/json-java-libraries-benchmark-jsonlib.html
Jackson Vs. Gson
And from the benchmark it looks like Jackson is the best and the fastest library for the JSON parsing...

Serialize as json string or binary

I need to persist a object. Unfortunately sqlite in android does not support ORM. Therefore I need to choose a different way to persist my data.
Is it better to persist the data via java serialization (ObjectOutputStream) within a blob, or via Gson's json serialization as a json string? Afterwards I need to serialize the data again as a json string in order to transmit it to a restful wcf service.
I'm worried about the serialization speed because afaik working with strings isn't very efficient.
In my experience serialization works faster than gson, but I haven't done any test on it. If speed is of concern I'd recommend you to do that test yourself.
On another note, you can use ORM on java/Android, read here: Any good ORM tools for Android development?

Speed up JSON objects

I'm using the org.json.* library for turning my web services' result (obviously json) into json objects. My problem is that the JSONObject and JSONArray constructors take a long time to build out the objects. I'm not passing a very large amount of data (anywhere between 1 and 100 array items with 3-5 keys each), but even with just 4 or 5 it takes a few seconds for the constructor to finish.
Is there a way to speed this up? Is there a faster library I could be using?
There's not a whole lot of code to show.
JSONArray arrayjson = new JSONArray(json);
Where json is a String.
You might give gson a try. This article seems to indicate that it has pretty good performance on Android vs the other alternatives. Jackson might be another good alternative.
According to the performance results at https://github.com/eishay/jvm-serializers/wiki, for serialization with databind with strings, e.g., gson.toJson(myObject), Gson is over 10X slower than Jackson. FastJSON beat Jackson at this same test by 2-3%.
Deserialization performance is similar, with Gson over 9x slower than Jackson, and FastJSON about 0.5% faster than Jackson.
Note: The current test results used Gson 1.6. With Gson 1.7.1, the databind performance improved 10-15% over Gson 1.6 (but the manual and manual/tree solutions showed no improvement). The results will hopefully be updated accordingly soon.

Categories

Resources