This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to branch maintenance
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-json.git
The following commit(s) were added to refs/heads/maintenance by this push:
new 6e91fdf SLING-12269 - Restore previous behaviour of JSONObject and
JSONArray getString methods (#3)
6e91fdf is described below
commit 6e91fdf5af4ecad6e00e4c6a16aad8685d19186b
Author: Remo Liechti <[email protected]>
AuthorDate: Wed Mar 20 13:08:28 2024 +0100
SLING-12269 - Restore previous behaviour of JSONObject and JSONArray
getString methods (#3)
---
README.md | 1 +
src/main/java/org/apache/sling/commons/json/JSONArray.java | 3 ++-
src/main/java/org/apache/sling/commons/json/JSONObject.java | 3 ++-
.../java/org/apache/sling/commons/json/JSONArrayTest.java | 8 +-------
src/test/java/org/apache/sling/commons/json/JSONMLTest.java | 11 +++--------
.../java/org/apache/sling/commons/json/JSONObjectTest.java | 11 +++--------
6 files changed, 12 insertions(+), 25 deletions(-)
diff --git a/README.md b/README.md
index c03ac4d..d675020 100644
--- a/README.md
+++ b/README.md
@@ -28,4 +28,5 @@ Manual changes applied to the library
- JSONWriter: add missing methods isTidy/setTidy
- make the following classes package-private, as those were not present in
the initial version of this library: JSONML, JSONMLParserConfioguration,
Property, JSONPointer, JSONPointerException, XMLParserConfiguration,
XMLXsiTypeConverter, JSONParserConfiguration
- check for newly added classes that add features to the json-java library,
whereas we do not want to add them to the API of this library
+ - to restore the old behaviour in reading string, adapt the getString()
methods in JSONObject and JSONArray. Instead of throwing an exception, return
get(...).toString() instead.
- build using mvn clean install, ensure that no more breaking changes are
present with mvn bundle:baseline
diff --git a/src/main/java/org/apache/sling/commons/json/JSONArray.java
b/src/main/java/org/apache/sling/commons/json/JSONArray.java
index dc85047..e962e0e 100644
--- a/src/main/java/org/apache/sling/commons/json/JSONArray.java
+++ b/src/main/java/org/apache/sling/commons/json/JSONArray.java
@@ -538,7 +538,8 @@ public class JSONArray implements Iterable<Object> {
if (object instanceof String) {
return (String) object;
}
- throw wrongValueFormatException(index, "String", object, null);
+ // SLING-12269: changed behaviour to match the old style functionality
+ return get(index).toString();
}
/**
diff --git a/src/main/java/org/apache/sling/commons/json/JSONObject.java
b/src/main/java/org/apache/sling/commons/json/JSONObject.java
index 11e27d1..fc7d607 100644
--- a/src/main/java/org/apache/sling/commons/json/JSONObject.java
+++ b/src/main/java/org/apache/sling/commons/json/JSONObject.java
@@ -915,7 +915,8 @@ public class JSONObject {
if (object instanceof String) {
return (String) object;
}
- throw wrongValueFormatException(key, "string", object, null);
+ // SLING-12269: changed behaviour to match the old style functionality
+ return get(key).toString();
}
/**
diff --git a/src/test/java/org/apache/sling/commons/json/JSONArrayTest.java
b/src/test/java/org/apache/sling/commons/json/JSONArrayTest.java
index 003bb2b..f15dc17 100644
--- a/src/test/java/org/apache/sling/commons/json/JSONArrayTest.java
+++ b/src/test/java/org/apache/sling/commons/json/JSONArrayTest.java
@@ -444,13 +444,7 @@ public class JSONArrayTest {
assertEquals("Expected an exception message",
"JSONArray[4] is not a long (class java.lang.String :
hello).", e.getMessage());
}
- try {
- jsonArray.getString(5);
- assertTrue("expected getString to fail", false);
- } catch (JSONException e) {
- assertEquals("Expected an exception message",
- "JSONArray[5] is not a String (class java.math.BigDecimal
: 0.002345).", e.getMessage());
- }
+ assertEquals("0.002345", jsonArray.getString(5));
Util.checkJSONArrayMaps(jsonArray);
}
diff --git a/src/test/java/org/apache/sling/commons/json/JSONMLTest.java
b/src/test/java/org/apache/sling/commons/json/JSONMLTest.java
index 1a78437..0938db2 100644
--- a/src/test/java/org/apache/sling/commons/json/JSONMLTest.java
+++ b/src/test/java/org/apache/sling/commons/json/JSONMLTest.java
@@ -133,14 +133,9 @@ public class JSONMLTest {
"]" +
"]";
JSONArray jsonArray = new JSONArray(jsonArrayStr);
- try {
- JSONML.toString(jsonArray);
- assertTrue("Expecting an exception", false);
- } catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "JSONArray[0] is not a String (class
org.apache.sling.commons.json.JSONArray).",
- e.getMessage());
- }
+ assertEquals(
+ "<addresses xsi:noNamespaceSchemaLocation=\"test.xsd\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><["name"]><nocontent/>></["name"]></addresses>",
+ JSONML.toString(jsonArray));
}
/**
diff --git a/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
b/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
index cc3856f..5fca6b7 100644
--- a/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
+++ b/src/test/java/org/apache/sling/commons/json/JSONObjectTest.java
@@ -1170,14 +1170,9 @@ public class JSONObjectTest {
"JSONObject[\"nonKey\"] not found.",
e.getMessage());
}
- try {
- jsonObject.getString("trueKey");
- fail("Expected an exception");
- } catch (JSONException e) {
- assertEquals("Expecting an exception message",
- "JSONObject[\"trueKey\"] is not a
string (class java.lang.Boolean : true).",
- e.getMessage());
- }
+
+ assertEquals("true", jsonObject.getString("trueKey"));
+
try {
jsonObject.getDouble("nonKey");
fail("Expected an exception");