felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1784269 - in /felix/trunk/utils/src: main/java/org/apache/felix/utils/json/JSONParser.java test/java/org/apache/felix/utils/json/JSONParserTest.java
Date Fri, 24 Feb 2017 12:11:38 GMT
Author: davidb
Date: Fri Feb 24 12:11:37 2017
New Revision: 1784269

URL: http://svn.apache.org/viewvc?rev=1784269&view=rev
Log:
FELIX-5555 JSONParser is not handling escape char properly

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=1784269&r1=1784268&r2=1784269&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 Fri Feb 24
12:11:37 2017
@@ -94,7 +94,7 @@ public class JSONParser {
             throw new IllegalArgumentException("Malformatted JSON key-value pair: " + jsonKeyValue);
         }
 
-        return new Pair<String, Object>(matcher.group(1), parseValue(matcher.group(2)));
+        return new Pair<String, Object>(unEscapeString(matcher.group(1)), parseValue(matcher.group(2)));
     }
 
     private static Object parseValue(String jsonValue) {
@@ -105,7 +105,7 @@ public class JSONParser {
             if (!jsonValue.endsWith("\""))
                 throw new IllegalArgumentException("Malformatted JSON string: " + jsonValue);
 
-            return jsonValue.substring(1, jsonValue.length() - 1);
+            return unEscapeString(jsonValue.substring(1, jsonValue.length() - 1));
         case '[':
             List<Object> entries = new ArrayList<Object>();
             for (String v : parseListValuesRaw(jsonValue)) {
@@ -130,6 +130,43 @@ public class JSONParser {
         }
     }
 
+    private static String unEscapeString(String s) {
+        StringBuilder sb = new StringBuilder(s);
+
+        for (int i = 0; i<sb.length(); i++) {
+            if (sb.charAt(i) == '\\' && sb.length() > i+1) {
+                sb.deleteCharAt(i);
+
+                char nextChar = sb.charAt(i);
+                switch (nextChar) {
+                case 'b':
+                    sb.setCharAt(i, '\b');
+                    break;
+                case 'f':
+                    sb.setCharAt(i, '\f');
+                    break;
+                case 'n':
+                    sb.setCharAt(i, '\n');
+                    break;
+                case 'r':
+                    sb.setCharAt(i, '\r');
+                    break;
+                case 't':
+                    sb.setCharAt(i, '\t');
+                    break;
+                case 'u':
+                    if (sb.length() > i+4) {
+                        int uc = Integer.parseInt(sb.substring(i+1, i+5), 16);
+                        sb.replace(i, i+5, "" + (char) uc);
+                    }
+                    break;
+                }
+            }
+        }
+
+        return sb.toString();
+    }
+
     private static Map<String, Object> parseObject(String jsonObject) {
         if (!(jsonObject.startsWith("{") && jsonObject.endsWith("}")))
             throw new IllegalArgumentException("Malformatted JSON object: " + jsonObject);
@@ -292,6 +329,4 @@ public class JSONParser {
         }
         return new String(bytes, encoding);
     }
-
-
 }
\ No newline at end of file

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=1784269&r1=1784268&r2=1784269&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 Fri Feb
24 12:11:37 2017
@@ -22,7 +22,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
@@ -77,9 +76,8 @@ public class JSONParserTest {
         assertEquals(result, m.get("abc"));
     }
 
-    @Ignore("FELIX-5555")
     @Test
-    public void escapeChar() throws Exception{
+    public void testEscapeChar() throws Exception{
         StringWriter sw = new StringWriter();
         JSONWriter js = new JSONWriter(sw);
         js.object().key("foo").value("/bar").endObject().flush();
@@ -87,4 +85,22 @@ public class JSONParserTest {
         JSONParser jp = new JSONParser(sw.toString());
         assertEquals("/bar", jp.getParsed().get("foo"));
     }
+
+    @Test
+    public void testEscapeChar2() throws Exception{
+        String s = "{\"tab\":\"\\t\","
+                + "\"quotes\": \"\\'\\\"\\\'\","
+                + "\"slashes\": \"\\/\\\\\\/\","
+                + "\"back\\bspace\": \"form\\ffeed\","
+                + "\"\\n\\n\\n\": \"\\r\\r\\r\","
+                + "\"!\\u2708!\\u2708!\": \"\\u2202\"}";
+        JSONParser jp = new JSONParser(s);
+        Map<String, Object> parsed = jp.getParsed();
+        assertEquals("\t", parsed.get("tab"));
+        assertEquals("\'\"\'", parsed.get("quotes"));
+        assertEquals("/\\/", parsed.get("slashes"));
+        assertEquals("form\ffeed", parsed.get("back\bspace"));
+        assertEquals("\r\r\r", parsed.get("\n\n\n"));
+        assertEquals("\u2202", parsed.get("!\u2708!\u2708!"));
+    }
 }



Mime
View raw message