In AEM 6.3,The JSONArray API is deprecated ,so what is the alternative in place of JSONArray API?
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With