cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7496] Avoiding indexOf searches
Date Tue, 05 Sep 2017 15:34:22 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes f5770688c -> 3c37b64cb


[CXF-7496] Avoiding indexOf searches


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3c37b64c
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3c37b64c
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3c37b64c

Branch: refs/heads/3.1.x-fixes
Commit: 3c37b64cb7b33acbb8c8f8e557b24f313914bed6
Parents: f577068
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue Sep 5 16:31:54 2017 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue Sep 5 16:34:02 2017 +0100

----------------------------------------------------------------------
 .../json/basic/JsonMapObjectReaderWriter.java   | 47 +++++++++-------
 .../basic/JsonMapObjectReaderWriterTest.java    | 56 ++++++++++++++++++++
 2 files changed, 84 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3c37b64c/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java
b/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java
index df4070e..39fe7dd 100644
--- a/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java
+++ b/rt/rs/extensions/json-basic/src/main/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriter.java
@@ -42,6 +42,7 @@ public class JsonMapObjectReaderWriter {
     private static final char OBJECT_END = '}';
     private static final char ARRAY_START = '[';
     private static final char ARRAY_END = ']';
+    private static final char ESCAPE = '\\';
     private static final String NULL_VALUE = "null";
     private boolean format;
     
@@ -197,7 +198,7 @@ public class JsonMapObjectReaderWriter {
                 values.put(name, internalFromJsonAsList(name, newJson));
                 i = closingIndex + 1;
             } else {
-                int commaIndex = getCommaIndex(json, sepIndex + j, sepIndex + j);
+                int commaIndex = getCommaIndex(json, sepIndex + j);
                 Object value = readPrimitiveValue(name, json, sepIndex + j, commaIndex);
                 values.put(name, value);
                 i = commaIndex + 1;
@@ -218,7 +219,7 @@ public class JsonMapObjectReaderWriter {
                 values.add(nextMap.map);
                 i = closingIndex + 1;
             } else {
-                int commaIndex = getCommaIndex(json, i, i);
+                int commaIndex = getCommaIndex(json, i);
                 Object value = readPrimitiveValue(name, json, i, commaIndex);
                 values.add(value);
                 i = commaIndex;
@@ -247,33 +248,41 @@ public class JsonMapObjectReaderWriter {
         return value;
     }
 
-    protected static int getCommaIndex(String json, int start, int from) {
-        int commaIndex = json.indexOf(COMMA, from);
+    protected static int getCommaIndex(String json, int from) {
+        int commaIndex = getNextSepCharIndex(json, COMMA, from);
         if (commaIndex == -1) {
             commaIndex = json.length();
-        } else if (json.charAt(commaIndex - 1) != DQUOTE && json.charAt(start) ==
DQUOTE) {
-            int lastNonWhiteCharIndex = commaIndex - 1; 
-            for (; lastNonWhiteCharIndex > from; lastNonWhiteCharIndex--) {
-                if (!Character.isWhitespace(json.charAt(lastNonWhiteCharIndex))) {
-                    break;
-                }
-            }
-            if (json.charAt(lastNonWhiteCharIndex) != DQUOTE) {
-                commaIndex = getCommaIndex(json, start, commaIndex + 1);
-            }
         }
         return commaIndex;
     }
-    protected int getClosingIndex(String json, char openChar, char closeChar, int from) {
-        int nextOpenIndex = json.indexOf(openChar, from + 1);
-        int closingIndex = json.indexOf(closeChar, from + 1);
+    protected static int getClosingIndex(String json, char openChar, char closeChar, int
from) {
+        int nextOpenIndex = getNextSepCharIndex(json, openChar, from + 1);
+        int closingIndex = getNextSepCharIndex(json, closeChar, from + 1);
         while (nextOpenIndex != -1 && nextOpenIndex < closingIndex) {
-            nextOpenIndex = json.indexOf(openChar, nextOpenIndex + 1);
-            closingIndex = json.indexOf(closeChar, closingIndex + 1);
+            nextOpenIndex = getNextSepCharIndex(json, openChar, nextOpenIndex + 1);
+            closingIndex = getNextSepCharIndex(json, closeChar, closingIndex + 1);
         }
         return closingIndex;
     }
     
+    protected static int getNextSepCharIndex(String json, char curlyBracketChar, int from)
{
+        int nextCurlyBracketIndex = -1;
+        boolean inString = false;
+        for (int i = from; i < json.length(); i++) {
+            char currentChar = json.charAt(i);
+            if (currentChar == curlyBracketChar && !inString) {
+                nextCurlyBracketIndex = i;
+                break;
+            } else if (currentChar == DQUOTE) {
+                if (i > from && json.charAt(i - 1) == ESCAPE) {
+                    continue;
+                }
+                inString = !inString;
+            }
+        }
+        return nextCurlyBracketIndex;
+    }
+    
     public void setFormat(boolean format) {
         this.format = format;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3c37b64c/rt/rs/extensions/json-basic/src/test/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriterTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/json-basic/src/test/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriterTest.java
b/rt/rs/extensions/json-basic/src/test/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriterTest.java
index aecad16..0dce1ef 100644
--- a/rt/rs/extensions/json-basic/src/test/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriterTest.java
+++ b/rt/rs/extensions/json-basic/src/test/java/org/apache/cxf/jaxrs/json/basic/JsonMapObjectReaderWriterTest.java
@@ -24,6 +24,8 @@ import java.util.Date;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.cxf.helpers.CastUtils;
+
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -68,4 +70,58 @@ public class JsonMapObjectReaderWriterTest extends Assert {
         assertEquals(Collections.singletonList("cValue1, cValue2"), map.get("c"));
         assertEquals("dValue1,dValue2,dValue3,dValue4", map.get("d"));
     }
+    @Test
+    public void testReadStringWithLeftCurlyBracketInString() throws Exception {
+        JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
+        String s = "{\"x\":{\"y\":\"{\"}}";
+        Map<String, Object> map = jsonMapObjectReaderWriter.fromJson(s);
+        assertEquals(1, map.size());
+        Map<String, Object> xMap = CastUtils.cast((Map<?, ?>)map.get("x"));
+        assertEquals(1, xMap.size());
+        assertEquals("{", xMap.get("y"));
+    }
+    @Test
+    public void testReadStringWithLeftCurlyBracketInString2() throws Exception {
+        JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
+        String s = "{\"x\":{\"y\":\"{\", \"z\":\"{\"}, \"a\":\"b\"}";
+        Map<String, Object> map = jsonMapObjectReaderWriter.fromJson(s);
+        assertEquals(2, map.size());
+        assertEquals("b", map.get("a"));
+        Map<String, Object> xMap = CastUtils.cast((Map<?, ?>)map.get("x"));
+        assertEquals(2, xMap.size());
+        assertEquals("{", xMap.get("y"));
+        assertEquals("{", xMap.get("z"));
+    }
+    @Test
+    public void testReadStringWithRightCurlyBracketInString() throws Exception {
+        JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
+        String s = "{\"x\":{\"y\":\"}\"}}";
+        Map<String, Object> map = jsonMapObjectReaderWriter.fromJson(s);
+        assertEquals(1, map.size());
+        Map<String, Object> xMap = CastUtils.cast((Map<?, ?>)map.get("x"));
+        assertEquals(1, xMap.size());
+        assertEquals("}", xMap.get("y"));
+    }
+    @Test
+    public void testReadStringWithRightCurlyBracketInString2() throws Exception {
+        JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
+        String s = "{\"x\":{\"y\":\"}\", \"z\":\"}\"}, \"a\":\"b\"}";
+        Map<String, Object> map = jsonMapObjectReaderWriter.fromJson(s);
+        assertEquals(2, map.size());
+        assertEquals("b", map.get("a"));
+        Map<String, Object> xMap = CastUtils.cast((Map<?, ?>)map.get("x"));
+        assertEquals(2, xMap.size());
+        assertEquals("}", xMap.get("y"));
+        assertEquals("}", xMap.get("z"));
+    }
+    @Test
+    public void testReadStringWithCurlyBracketsInString() throws Exception {
+        JsonMapObjectReaderWriter jsonMapObjectReaderWriter = new JsonMapObjectReaderWriter();
+        String s = "{\"x\":{\"y\":\"{\\\"}\"}}";
+        Map<String, Object> map = jsonMapObjectReaderWriter.fromJson(s);
+        assertEquals(1, map.size());
+        Map<String, Object> xMap = CastUtils.cast((Map<?, ?>)map.get("x"));
+        assertEquals(1, xMap.size());
+        assertEquals("{\\\"}", xMap.get("y"));
+    }
 }


Mime
View raw message