Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONObject vs. javax.json.JsonObject?

What are the significant diffs between:

org.json.JSONObject and javax.json.JsonObject?

Most importantly are they interchangeable from client to Webservice? ie. can I send JSONObject to Webservice and have the Webservice believe the type is JsonObject (and vice versa)?

(JSONObject found in the json-20080701.jar of ACRA)

(JsonObject found in C:\glassfish4\glassfish\modules\javax.json.jar)

like image 562
JDOaktown Avatar asked Feb 23 '15 22:02

JDOaktown


1 Answers

What are the significant diffs between org.json.JSONObject and javax.json.JsonObject?

  • javax.json.JsonObject is included in Java EE 7
  • javax.json.JsonObject is immutable
  • org.json.JSONObject has siginificantly more convenience methods

Most importantly are they interchangeable from client to Webservice? ie. can I send JSONObject to Webservice and have the Webservice believe the type is JsonObject (and vice versa)?

Of course this should work. It is not the class instance which gets transferred to the webservice, but the JSON data, which is generated from the class instance. On the other side, the JSON data can be parsed back into any kind of object.

Example:

If you have a simple class named Person:

public class Person {

 private String name = "Hans";
 private int age = 26;
}

This could be transformed into JSON similar to: {"name":"Hans", "age":25}

The generated JSON string is sent to the webservice.

Now, on the other end of your application, or in any other application, this JSON string can be parsed into any class, if you have an appropriate parser. You don't even need Java to parse it.

like image 177
unwichtich Avatar answered Sep 22 '22 20:09

unwichtich