org.apache.sling.commons.json.JSONArray is deprecated in AEM 6.3
Asked Answered
M

4

6

In AEM 6.3,The JSONArray API is deprecated ,so what is the alternative in place of JSONArray API?

Must answered 27/6, 2017 at 11:17 Comment(3)
Can you provide a link to this deprecated API?Passable
docs.adobe.com/docs/en/aem/6-3/develop/ref/javadoc/…Discant
I answered this question in one of my blog:sgaem.blogspot.in/2017/08/…Must
B
4

The best way forward is to change your Json API from org.apache.sling.commons.json to com.google.gson. Since it is already being used in AEM elsewhere.

GSON Usage in AEM

Becoming answered 6/7, 2017 at 17:55 Comment(1)
the org.json package is also available in AEM already. As the answer below (https://mcmap.net/q/1646044/-org-apache-sling-commons-json-jsonarray-is-deprecated-in-aem-6-3) explains this would be an easier package to transition to.Penland
S
7

Due to license issues these classes were removed with Sling 9. It simply was the json.org library. Sling itself used it for a few things only, so Sling was refactored to get along without it.

I afraid, there is no replacement. You have to choose a different JSON library, include it into your project and port your code. As the JSON lib is pretty straight forward, it should be doable.

Here some links with the mailing lists:

https://lists.apache.org/thread.html/ee51bace078681765d5dcfeda1939628ccefb9b4261b1d7f6a56d420@%3Cdev.sling.apache.org%3E

http://mail-archives.apache.org/mod_mbox/www-legal-discuss/201611.mbox/browser

https://issues.apache.org/jira/browse/SLING-6536

Here is the license in question. It contains the ambiguous sentence "The Software shall be used for Good, not Evil."

https://github.com/stleary/JSON-java/blob/master/LICENSE

Stanhope answered 27/6, 2017 at 13:23 Comment(1)
Nice Explanation.Must
B
4

The best way forward is to change your Json API from org.apache.sling.commons.json to com.google.gson. Since it is already being used in AEM elsewhere.

GSON Usage in AEM

Becoming answered 6/7, 2017 at 17:55 Comment(1)
the org.json package is also available in AEM already. As the answer below (https://mcmap.net/q/1646044/-org-apache-sling-commons-json-jsonarray-is-deprecated-in-aem-6-3) explains this would be an easier package to transition to.Penland
C
1

Another way forward which avoids (most) code changes is to replace the Sling Commons JSON library with Org.Json if you can live with the license change that caused it be deprecated in the first place.

Add to the pom.xml dependencies:

You can add the org.json dependency to the pom.xml:

<dependency>
 <groupId>org.json</groupId>
 <artifactId>json</artifactId>
 <version>20180813</version>
</dependency>

<!-- Or depending on version of AEM 
     use the granite bundled version instead -->

<dependency>
    <artifactId>json</artifactId>
    <version>20090211_1</version>
    <groupId>com.adobe.granite.bundles</groupId>
    <scope>provided</scope>
</dependency>

OR use Open-JSON, a clean-room reimplementation by the Android team which has a standard Apache 2.0 license without the 'evil' clause:

<dependency>
    <groupId>com.tdunning</groupId>
    <artifactId>json</artifactId>
    <version>1.8</version>
</dependency>

Update Import statements

The Apache Sling Commons JSON is mostly a repackaged org.json parser, and appears to be compatible aside from package path differences. To change to the org.json or Open-Json parser, alter these imports:

import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;

to

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;

For many this will be an acceptable and clean solution that doesn't require reworking all the code to use GSON or Jackson (both available in AEM).

Caveat: I've only tested this compiles... You might need to embed the parser in your bundle. Neither of these parsers claim to be OSGi aware.

Caulicle answered 12/4, 2019 at 10:41 Comment(0)
C
1

You can use any json API depending upon your requirement. I will suggest using Gson as its quite easy to use as one can map a json object directly to a pojo class and then use the object of the pojo class. It removes a lot of boilerplate code for reading the json objects one by one.

Calculous answered 12/4, 2019 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.