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 3094b49  SLING-12304 establish backwards compatibility for insertion 
order in JSONObject (#5)
3094b49 is described below

commit 3094b4944601f8a2f58182afdd2c1598f3883bb2
Author: Remo Liechti <[email protected]>
AuthorDate: Thu Apr 25 13:16:07 2024 +0200

    SLING-12304 establish backwards compatibility for insertion order in 
JSONObject (#5)
---
 README.md                                          |  1 +
 .../org/apache/sling/commons/json/JSONObject.java  | 12 +++---
 .../json/JSONWriterTestBackwardsCompatibility.java | 46 ++++++++++++++++++++++
 3 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index 2d18c7f..0b7bd57 100644
--- a/README.md
+++ b/README.md
@@ -31,4 +31,5 @@ Manual changes applied to the 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.
     - in JSONWriter.key(), remove the check that throws an exception for 
duplicated keys. This means you also must remove the check in JSONObject. The 
testclass JSONWriterTestBackwardsCompatibility must suceed. Remove  the test 
cases from JSONObjectTest.
+    - in JSONObject, replace all occurances of HashMap with LinkedHashMap, see 
https://issues.apache.org/jira/browse/SLING-12304
 - 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/JSONObject.java 
b/src/main/java/org/apache/sling/commons/json/JSONObject.java
index 01b95c0..c1c29d8 100644
--- a/src/main/java/org/apache/sling/commons/json/JSONObject.java
+++ b/src/main/java/org/apache/sling/commons/json/JSONObject.java
@@ -18,9 +18,9 @@ import java.math.BigInteger;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -177,7 +177,7 @@ public class JSONObject {
         // implementations to rearrange their items for a faster element
         // retrieval based on associative access.
         // Therefore, an implementation mustn't rely on the order of the item.
-        this.map = new HashMap<String, Object>();
+        this.map = new LinkedHashMap<String, Object>();
     }
 
     /**
@@ -306,9 +306,9 @@ public class JSONObject {
                     "JSONObject has reached recursion depth limit of " + 
jsonParserConfiguration.getMaxNestingDepth());
         }
         if (m == null) {
-            this.map = new HashMap<String, Object>();
+            this.map = new LinkedHashMap<String, Object>();
         } else {
-            this.map = new HashMap<String, Object>(m.size());
+            this.map = new LinkedHashMap<String, Object>(m.size());
             for (final Entry<?, ?> e : m.entrySet()) {
                 if (e.getKey() == null) {
                     throw new NullPointerException("Null key.");
@@ -501,7 +501,7 @@ public class JSONObject {
      * @param initialCapacity initial capacity of the internal map.
      */
     protected JSONObject(int initialCapacity) {
-        this.map = new HashMap<String, Object>(initialCapacity);
+        this.map = new LinkedHashMap<String, Object>(initialCapacity);
     }
 
     /**
@@ -3015,7 +3015,7 @@ public class JSONObject {
      * @return a java.util.Map containing the entries of this object
      */
     public Map<String, Object> toMap() {
-        Map<String, Object> results = new HashMap<String, Object>();
+        Map<String, Object> results = new LinkedHashMap<String, Object>();
         for (Entry<String, Object> entry : this.entrySet()) {
             Object value;
             if (entry.getValue() == null || NULL.equals(entry.getValue())) {
diff --git 
a/src/test/java/org/apache/sling/commons/json/JSONWriterTestBackwardsCompatibility.java
 
b/src/test/java/org/apache/sling/commons/json/JSONWriterTestBackwardsCompatibility.java
index 177fc47..fdfff13 100644
--- 
a/src/test/java/org/apache/sling/commons/json/JSONWriterTestBackwardsCompatibility.java
+++ 
b/src/test/java/org/apache/sling/commons/json/JSONWriterTestBackwardsCompatibility.java
@@ -17,8 +17,11 @@
 package org.apache.sling.commons.json;
 
 import java.io.StringWriter;
+import java.util.Iterator;
+
 import org.apache.sling.commons.json.io.JSONWriter;
 import org.apache.sling.commons.json.util.DespacedRendering;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -44,4 +47,47 @@ public class JSONWriterTestBackwardsCompatibility {
                 "_key_:_value2_");
     }
 
+    /**
+     * tests https://issues.apache.org/jira/browse/SLING-12304
+     * needs JSONObject to use LinkedHashMap instead of HashMap
+     */
+    @Test
+    public void ensureObjectOrder1() throws Exception {
+        JSONObject o = new JSONObject();
+        o.put("Hallo", "World");
+        o.put("foo", "bar");
+        o.put("welcome", "home");
+        o.put("fix", "my-order");
+
+        StringBuilder b = new StringBuilder();
+        Iterator<String> keys = o.keys();
+        while (keys.hasNext()) {
+            String key = keys.next();
+            b.append(key).append("=").append(o.get(key)).append(";");
+        }
+
+        Assert.assertEquals("Hallo=World;foo=bar;welcome=home;fix=my-order;", 
b.toString());
+    }
+
+    /**
+     * tests https://issues.apache.org/jira/browse/SLING-12304
+     * needs JSONObject to use LinkedHashMap instead of HashMap
+     */
+    @Test
+    public void ensureObjectOrder2() throws Exception {
+        JSONObject o = new JSONObject();
+        o.put("fix", "my-order");
+        o.put("Hallo", "World");
+        o.put("foo", "bar");
+        o.put("welcome", "home");
+
+        StringBuilder b = new StringBuilder();
+        Iterator<String> keys = o.keys();
+        while (keys.hasNext()) {
+            String key = keys.next();
+            b.append(key).append("=").append(o.get(key)).append(";");
+        }
+
+        Assert.assertEquals("fix=my-order;Hallo=World;foo=bar;welcome=home;", 
b.toString());
+    }
 }
\ No newline at end of file

Reply via email to