Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-76000

JSONObject wrapper misses the org.json.JSONObject.NULL

    Details

      Description

      The interface com.liferay.portal.kernel.json.JSONObject  and its implementation com.liferay.portlet.json.JSONObjectImpl (a wrapper to the org.json's library)  does not provide the JSONObject.NULL (but the native's org.json does).

      This is a potential issue because actually there is no way using the Liferay json APIs to serialize a JSONObject instance having one or more null values, because each null Java object reference will be ignored by the toJSONString() method as well as the related entry keys.

      The original org.json.JSONObject implementation, instead, provides a JSONObject.NULL static final field, that will be converted in the null value during the serialization in toJSONString().
      Liferay APIs, instead, does not provide the JSONObject.NULL so the Liferay APIs user has no way to use it.

      NOTE: null values are JSON-standard and this may be a potential problem when you have to call, for instance, a remote service that requires a particular field as mandatory in the serialized JSON request. For more informations about that issue please see: http://www.json.org and the first lines of comment of the original JSONObject implementation: https://github.com/stleary/JSON-java/blob/master/JSONObject.java.

      By default, the org.json.JSONObject implementation treats the null references as the same as JavaScript 'undefined'. You can check this behaviour with these few lines of code:

       JSONObject jo = JSONFactoryUtil.createJSONObject();

      jo.put("one", (JSONObject) null);
      jo.put("two", 2);
      String s = jo.toJSONString();
      
      System.out.println(s);
       
      

      The output will be:

      { "two": 2 }

      instead of the expected one:
        

      { "one": null, "two": 2 }

       

      So the only way to get the null object reference in JSON serialization using the com.liferay.portal.kernel.json APIs, is to provide a wrapper for the org.json.JSONObject.NULL field in the JSONFactoryUtil, for example, so the user can put this reference as value in the JSONObject.put(key, value).

       

       

       

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                2 years, 11 weeks, 2 days ago

                Packages

                Version Package
                Master