org.apache.sling.commons.json.JSONArray 在 AEM 6.3 中已弃用

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

在 AEM 6.3 中,JSONArray API 已弃用,那么替代 JSONArray API 的替代方法是什么?

由于许可问题,这些 类 已随 Sling 9 一起删除。它只是 json.org 库。 Sling 本身仅将它用于一些事情,因此 Sling 进行了重构以使其在没有它的情况下也能正常工作。

恐怕没有替代品了。您必须选择一个不同的 JSON 库,将其包含到您的项目中并移植您的代码。由于 JSON 库非常简单,它应该是可行的。

这里有一些邮件列表的链接:

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

这是有问题的许可证。它包含模棱两可的句子“该软件应用于善良,而不是邪恶。

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

最好的方法是将您的 Json API 从 org.apache.sling.commons.json 更改为 com.google.gson。因为它已在其他地方的 AEM 中使用。

另一种避免(大部分)代码更改的方法是将 Sling Commons JSON 库替换为 Org.Json 如果您可以忍受导致它的许可证更改首先弃用

加入pom.xml依赖:

您可以将 org.json 依赖项添加到 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>

或使用 Open-JSON,Android 团队的洁净室重新实现,该团队拥有标准的 Apache 2.0 许可证,没有 'evil' 条款:

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

更新导入语句

Apache Sling Commons JSON 主要是重新打包的 org.json 解析器,除了包路径差异外似乎是兼容的。要更改为 org.json 或 Open-Json 解析器,请更改这些导入:

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;

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

对于许多人来说,这将是一个可接受且干净的解决方案,不需要重新编写所有代码即可使用 GSON 或 Jackson(均在 AEM 中提供)。

警告:我只测试了这个编译...您可能需要将解析器嵌入到您的包中。这些解析器都没有声称支持 OSGi。

您可以根据需要使用任何 json API。我会建议使用 Gson,因为它非常容易使用,因为可以将 json 对象直接映射到 pojo class,然后使用 pojo class 的对象。它删除了大量用于逐一读取 json 对象的样板代码。