Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONObject vs Gson library JsonObject

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?

like image 652
iamcrypticcoder Avatar asked Mar 07 '17 06:03

iamcrypticcoder


People also ask

What is difference between JSON and GSON?

GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.

What is the difference between org JSON JSONObject and org JSON simple JSONObject?

simple package contains important classes like JSONValue, JSONObject, JSONArray, JsonString and JsonNumber. We need to install the json-simple. jar file to execute a JSON program whereas org. json library has classes to parse JSON for Java.

What is the difference between JSONObject and JSONObject?

JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one. I had some issue with the date formatting in JSONObject.

Which Java JSON library is best?

1. Jackson. Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers.


2 Answers

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.

like image 52
Mehmood Memon Avatar answered Oct 11 '22 08:10

Mehmood Memon


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
like image 36
GrabNewTech Avatar answered Oct 11 '22 09:10

GrabNewTech