Reputation: 2879
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?
Upvotes: 41
Views: 38668
Reputation: 857
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.
Upvotes: 2
Reputation: 641
Many JSON implementations are available in the market and most of them are open source. Each one has specific advantages and disadvantages.
Google GSON click for official documents
Jackson click for official documents
Some comparison blogs click here blogs1, blog2
I personally done a benchmark for serialization and deserialization using GSON vs Jackson vs Simple JSON
Upvotes: 16
Reputation: 1139
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.
Upvotes: 24