org.json.JSONObject and the Gson JsonObject library

What are the differences between the two classes?

If someone uses the Gson library, is it preferable to use com.google.json.JsonObject org.json.JSONObject ?

Can anyone list the pros and cons of these 2 options?

+14
json android gson android-gson android-json
source share
2 answers

Many JSON implementations are available on the market, and most of them are open source. Each of them has certain advantages and disadvantages.

  • Google gson
  • Jackson
  • org.json etc.

Google GSON click for white papers

  • Provide simple toJson () and fromJson () methods for converting Java objects to JSON and vice versa
  • Allow the creation of previously unmodifiable objects for conversion to and from JSON
  • Expanded Java Generics Support
  • Allow custom views for objects
  • Support for arbitrarily complex objects (with deep inheritance hierarchies and widespread use of generic types)

Jackson click for white papers

  • Streaming API or incremental parsing / generation: reads and writes JSON content as discrete events
  • Tree model: provides a mutable tree as a JSON document tree.
  • Data binding: converts JSON to and from POJO.

Some comparative blogs click here blogs1 , blog2

I personally did a test to serialize and deserialize using GSON vs Jackson vs Simple JSON

  • Very small object: Google gson is faster than Jackson and Simple JSON
  • Large objects: Google gson is faster than Jackson and Simple JSON
+7
source share

The main differences are listed below:

1) GSON can use the definition of Object to directly create an object of the required type. JSONObject must be parsed manually.

2) org.json - a simple tree-style API. The 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 can be inefficient.

3) The biggest weakness of the org.json implementation is the JSONException. It is simply not convenient to place a try / catch block around all your JSON files.

4) Gson is the best API for parsing JSON on Android. It has a very small binary size (up to 200 KiB), performs fast data binding and has a simple easy-to-use API.

5). GSON and Jackson are the most popular JSON data management solutions in the Java world.

+3
source share

All Articles