Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one to use? JSONObject from org.json VS JsonObject from javax.json

Tags:

java

json

android

This is my first post. As a budding Android developer, I read SO posts on a near daily basis on various topics, but for this question, I didn't find any help from Google or SO.

My research so far:
Searching for this question was harder than normal because the search engines don't seem to care about case-sensitivity, which is vital in this issue. Searching Google only gave me links to the classes themselves, old articles, or completely irrelevant articles. The closest I got was JSONArray vs JSONObject, and that is a completely different question. SO searching gave the same results. As far as I can tell, ALL pertinent posts on SO refer to JSONObject and not JsonObject.

Neither Oracle nor json.org documentation mentioned the other side, nor did the Android developer JSONObject page which uses the org.json library.

http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html
http://www.json.org/javadoc/org/json/JSONObject.html
I would have also posted a link to the Android reference page for JSONObject, but my rep as a newbie is too low.

History of problem(s): I was reading about Json encoding and decoding from the Oracle page, and I tried copying and pasting into my AndriodStudio IDE, which immediately produced an error, for which it did not suggest an import statement like it normally does. I have come to learn that means the class is not part of the built-in libraries.

The problem was that the code I pasted in used JsonObject (which comes from javax.json library) and not JSONObject (which comes from org.json library). When I noticed the difference in case between the Android page and the Oracle page, I altered my code from Json to JSON and immediately my IDE offered the org.json import statement.

Question(s): Why is an Oracle made class not part of the default libraries when an external library is?
Is JSON the old, deprecated way and Json the new, proper way?
Are they functionally identical? If not, please provide examples?
Are some situations better for one and some better for the other?
ULTIMATELY, which should I use, and why (if not already covered)?
If, javax.json, where is the best (safest) place to download that library)?

like image 776
Phyxius81 Avatar asked Mar 13 '15 21:03

Phyxius81


People also ask

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 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.

Is org JSON deprecated?

We know in AEM 6.3, all json related operations are deprecated from package org.

What is javax JSON?

The javax. json package provides an Object Model API to process JSON. The Object Model API is a high-level API that provides immutable object models for JSON object and array structures. These JSON structures can be represented as object models using JsonObject and JsonArray interfaces.


1 Answers

Bit late, but I wanted to share my opinion on this.

I faced this problem recently when I found a Java project with both libraries and they were used at the same time.

I think that org.json is easier to read and to use, for 2 main reasons (for my needs):

  1. JsonObject is immutable. You can't add new key/value pairs to an already existing JsonObject (reference here: javax.json: Add new JsonNumber to existing JsonObject)

  2. It takes a few lines to pretty print a JsonObject or JsonArray, while it only takes 1 line to do it with JSONObject or JSONArray. Example:

    StringWriter sw = new StringWriter(); Map<String, Object> properties = new HashMap<>(); properties.put(JsonGenerator.PRETTY_PRINTING, true);  JsonWriterFactory writerFactory = Json.createWriterFactory(properties); JsonWriter jsonWriter = writerFactory.createWriter(sw);  jsonWriter.writeObject(jsonObject); //JsonObject created before jsonWriter.close(); String prettyPrintedJSON = sw.toString(); 

That is the code I use to get an indented JSON to write to a file. And with org.json I only need jsonObject.toString(4).

Another difference is the constructors. You will need a JsonObjectBuilder to create a JSON with javax.json. One step more that can be avoided.

I'm sure there are more differences (not sure if it's possible to create a JsonObject from a String) but these are my thoughts.

like image 182
Samuel Méndez Avatar answered Oct 05 '22 07:10

Samuel Méndez