33

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?

Mark Amery
  • 110,735
  • 57
  • 354
  • 402
iamcrypticcoder
  • 1,513
  • 4
  • 17
  • 37

2 Answers2

15

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.

Mehmood Memon
  • 889
  • 1
  • 10
  • 20
  • 3
    Under 200KiB is not small at all for a JSON parser. in that regard, org.json latest version as of today is 65KiB, quite a lot smaller. – Renato Apr 21 '20 at 16:45
13

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
Dietatko
  • 378
  • 1
  • 13
GrabNewTech
  • 583
  • 4
  • 14