org.json.JSONObject vs Gson library JsonObject

左心房为你撑大大i 提交于 2019-12-20 17:24:44

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/42641301/org-json-jsonobject-vs-gson-library-jsonobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!