felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1783803 - in /felix/trunk/utils/src: main/java/org/apache/felix/utils/json/JSONParser.java test/java/org/apache/felix/utils/json/JSONParserTest.java
Date Mon, 20 Feb 2017 22:07:57 GMT
Author: sseifert
Date: Mon Feb 20 22:07:57 2017
New Revision: 1783803

URL: http://svn.apache.org/viewvc?rev=1783803&view=rev
Log:
FELIX-5556 JSONParser does not retain object order

Modified:
    felix/trunk/utils/src/main/java/org/apache/felix/utils/json/JSONParser.java
    felix/trunk/utils/src/test/java/org/apache/felix/utils/json/JSONParserTest.java

Modified: felix/trunk/utils/src/main/java/org/apache/felix/utils/json/JSONParser.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/json/JSONParser.java?rev=1783803&r1=1783802&r2=1783803&view=diff
==============================================================================
--- felix/trunk/utils/src/main/java/org/apache/felix/utils/json/JSONParser.java (original)
+++ felix/trunk/utils/src/main/java/org/apache/felix/utils/json/JSONParser.java Mon Feb 20
22:07:57 2017
@@ -21,7 +21,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Stack;
@@ -134,7 +134,7 @@ public class JSONParser {
         if (!(jsonObject.startsWith("{") && jsonObject.endsWith("}")))
             throw new IllegalArgumentException("Malformatted JSON object: " + jsonObject);
 
-        Map<String, Object> values = new HashMap<String, Object>();
+        Map<String, Object> values = new LinkedHashMap<String, Object>();
 
         jsonObject = jsonObject.substring(1, jsonObject.length() - 1).trim();
         if (jsonObject.length() == 0)

Modified: felix/trunk/utils/src/test/java/org/apache/felix/utils/json/JSONParserTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/json/JSONParserTest.java?rev=1783803&r1=1783802&r2=1783803&view=diff
==============================================================================
--- felix/trunk/utils/src/test/java/org/apache/felix/utils/json/JSONParserTest.java (original)
+++ felix/trunk/utils/src/test/java/org/apache/felix/utils/json/JSONParserTest.java Mon Feb
20 22:07:57 2017
@@ -16,18 +16,20 @@
  */
 package org.apache.felix.utils.json;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 import java.io.StringWriter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.junit.Ignore;
 import org.junit.Test;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
 public class JSONParserTest {
     @Test
     public void testJsonSimple() {
@@ -77,6 +79,27 @@ public class JSONParserTest {
         assertEquals(result, m.get("abc"));
     }
 
+    @Test
+    public void testJsonOrder() {
+        String json = "{\"prop5\":1,\"prop1\":2,\"prop6\":3,\"prop2\":4,\"prop4\":5,\"prop3\":6}";
+        JSONParser jp = new JSONParser(json);
+        Map<String, Object> m = jp.getParsed();
+        assertEquals(6, m.size());
+        
+        List<Map.Entry<String,Object>> entries = new ArrayList<Map.Entry<String,Object>>(m.entrySet());
+        assertEntry(entries, 0, "prop5", 1L);
+        assertEntry(entries, 1, "prop1", 2L);
+        assertEntry(entries, 2, "prop6", 3L);
+        assertEntry(entries, 3, "prop2", 4L);
+        assertEntry(entries, 4, "prop4", 5L);
+        assertEntry(entries, 5, "prop3", 6L);
+    }
+    private void assertEntry(List<Map.Entry<String,Object>> list, int index,
String key, Object value) {
+        Map.Entry<String,Object> entry = list.get(index);
+        assertEquals(key, entry.getKey());
+        assertEquals(value, entry.getValue());
+    }
+
     @Ignore("FELIX-5555")
     @Test
     public void escapeChar() throws Exception{



Mime
View raw message