You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2017/03/21 13:00:43 UTC

cxf git commit: [CXF-7287] Minor optimizations

Repository: cxf
Updated Branches:
  refs/heads/master 6613e4666 -> ae3ae2a25


[CXF-7287] Minor optimizations


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

Branch: refs/heads/master
Commit: ae3ae2a25109375cc82114d15078f04e8c5353c5
Parents: 6613e46
Author: Sergey Beryozkin <sb...@gmail.com>
Authored: Tue Mar 21 13:00:11 2017 +0000
Committer: Sergey Beryozkin <sb...@gmail.com>
Committed: Tue Mar 21 13:00:11 2017 +0000

----------------------------------------------------------------------
 .../json/basic/JsonMapObjectReaderWriter.java   | 84 +++++++++++++-------
 1 file changed, 54 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ae3ae2a2/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 89d502d..4e87d78 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
@@ -35,6 +35,13 @@ import org.apache.cxf.helpers.IOUtils;
 
 
 public class JsonMapObjectReaderWriter {
+    private static final char DQUOTE = '"';
+    private static final char COMMA = ',';
+    private static final char COLON = ':';
+    private static final char OBJECT_START = '{';
+    private static final char OBJECT_END = '}';
+    private static final char ARRAY_START = '[';
+    private static final char ARRAY_END = ']';
     private static final String NULL_VALUE = "null";
     private boolean format;
 
@@ -70,14 +77,14 @@ public class JsonMapObjectReaderWriter {
     }
 
     protected void toJsonInternal(Output out, Map<String, Object> map) {
-        out.append("{");
+        out.append(OBJECT_START);
         for (Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext();) {
             Map.Entry<String, Object> entry = it.next();
-            out.append("\"").append(entry.getKey()).append("\"");
-            out.append(":");
+            out.append(DQUOTE).append(entry.getKey()).append(DQUOTE);
+            out.append(COLON);
             toJsonInternal(out, entry.getValue(), it.hasNext());
         }
-        out.append("}");
+        out.append(OBJECT_END);
     }
 
     protected void toJsonInternal(Output out, Object[] array) {
@@ -85,13 +92,13 @@ public class JsonMapObjectReaderWriter {
     }
 
     protected void toJsonInternal(Output out, Collection<?> coll) {
-        out.append("[");
+        out.append(ARRAY_START);
         formatIfNeeded(out);
         for (Iterator<?> iter = coll.iterator(); iter.hasNext();) {
             toJsonInternal(out, iter.next(), iter.hasNext());
         }
         formatIfNeeded(out);
-        out.append("]");
+        out.append(ARRAY_END);
     }
 
     @SuppressWarnings("unchecked")
@@ -109,15 +116,15 @@ public class JsonMapObjectReaderWriter {
         } else {
             boolean quotesNeeded = checkQuotesNeeded(value);
             if (quotesNeeded) {
-                out.append("\"");
+                out.append(DQUOTE);
             }
             out.append(value.toString());
             if (quotesNeeded) {
-                out.append("\"");
+                out.append(DQUOTE);
             }
         }
         if (hasNext) {
-            out.append(",");
+            out.append(COMMA);
             formatIfNeeded(out);
         }
 
@@ -164,28 +171,28 @@ public class JsonMapObjectReaderWriter {
     }
     protected void readJsonObjectAsSettable(Settable values, String json) {
         for (int i = 0; i < json.length(); i++) {
-            if (isWhiteSpace(json.charAt(i))) {
+            if (Character.isWhitespace(json.charAt(i))) {
                 continue;
             }
 
-            int closingQuote = json.indexOf('"', i + 1);
-            int from = json.charAt(i) == '"' ? i + 1 : i;
+            int closingQuote = json.indexOf(DQUOTE, i + 1);
+            int from = json.charAt(i) == DQUOTE ? i + 1 : i;
             String name = json.substring(from, closingQuote);
-            int sepIndex = json.indexOf(':', closingQuote + 1);
+            int sepIndex = json.indexOf(COLON, closingQuote + 1);
 
             int j = 1;
-            while (isWhiteSpace(json.charAt(sepIndex + j))) {
+            while (Character.isWhitespace(json.charAt(sepIndex + j))) {
                 j++;
             }
-            if (json.charAt(sepIndex + j) == '{') {
-                int closingIndex = getClosingIndex(json, '{', '}', sepIndex + j);
+            if (json.charAt(sepIndex + j) == OBJECT_START) {
+                int closingIndex = getClosingIndex(json, OBJECT_START, OBJECT_END, sepIndex + j);
                 String newJson = json.substring(sepIndex + j + 1, closingIndex);
                 MapSettable nextMap = new MapSettable();
                 readJsonObjectAsSettable(nextMap, newJson);
                 values.put(name, nextMap.map);
                 i = closingIndex + 1;
-            } else if (json.charAt(sepIndex + j) == '[') {
-                int closingIndex = getClosingIndex(json, '[', ']', sepIndex + j);
+            } else if (json.charAt(sepIndex + j) == ARRAY_START) {
+                int closingIndex = getClosingIndex(json, ARRAY_START, ARRAY_END, sepIndex + j);
                 String newJson = json.substring(sepIndex + j + 1, closingIndex);
                 values.put(name, internalFromJsonAsList(name, newJson));
                 i = closingIndex + 1;
@@ -201,11 +208,11 @@ public class JsonMapObjectReaderWriter {
     protected List<Object> internalFromJsonAsList(String name, String json) {
         List<Object> values = new LinkedList<Object>();
         for (int i = 0; i < json.length(); i++) {
-            if (isWhiteSpace(json.charAt(i))) {
+            if (Character.isWhitespace(json.charAt(i))) {
                 continue;
             }
-            if (json.charAt(i) == '{') {
-                int closingIndex = getClosingIndex(json, '{', '}', i);
+            if (json.charAt(i) == OBJECT_START) {
+                int closingIndex = getClosingIndex(json, OBJECT_START, OBJECT_END, i);
                 MapSettable nextMap = new MapSettable();
                 readJsonObjectAsSettable(nextMap, json.substring(i + 1, closingIndex));
                 values.add(nextMap.map);
@@ -223,7 +230,7 @@ public class JsonMapObjectReaderWriter {
     protected Object readPrimitiveValue(String name, String json, int from, int to) {
         Object value = json.substring(from, to);
         String valueStr = value.toString().trim();
-        if (valueStr.startsWith("\"")) {
+        if (valueStr.charAt(0) == DQUOTE) {
             value = valueStr.substring(1, valueStr.length() - 1);
         } else if ("true".equals(valueStr) || "false".equals(valueStr)) {
             value = Boolean.valueOf(valueStr);
@@ -240,12 +247,17 @@ public class JsonMapObjectReaderWriter {
     }
 
     protected static int getCommaIndex(String json, int start, int from) {
-        int commaIndex = json.indexOf(",", from);
+        int commaIndex = json.indexOf(COMMA, from);
         if (commaIndex == -1) {
             commaIndex = json.length();
-        } else if (json.charAt(commaIndex - 1) != '\"' && json.charAt(start) == '\"') {
-            String value = json.substring(0, commaIndex).trim();
-            if (value.lastIndexOf("\"") != value.length() - 1) {
+        } 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);
             }
         }
@@ -260,10 +272,7 @@ public class JsonMapObjectReaderWriter {
         }
         return closingIndex;
     }
-    protected boolean isWhiteSpace(char jsonChar) {
-        return jsonChar == ' ' || jsonChar == '\r' || jsonChar == '\n' || jsonChar == '\t';
-    }
-
+    
     public void setFormat(boolean format) {
         this.format = format;
     }
@@ -289,6 +298,7 @@ public class JsonMapObjectReaderWriter {
     }
     private interface Output {
         Output append(String str);
+        Output append(char ch);
     }
     private class StringBuilderOutput implements Output {
         private StringBuilder sb;
@@ -300,6 +310,11 @@ public class JsonMapObjectReaderWriter {
             sb.append(str);
             return this;
         }
+        @Override
+        public Output append(char ch) {
+            sb.append(ch);
+            return this;
+        }
 
     }
     private class StreamOutput implements Output {
@@ -319,6 +334,15 @@ public class JsonMapObjectReaderWriter {
             }
             return this;
         }
+        @Override
+        public Output append(char ch) {
+            try {
+                os.write(ch);
+            } catch (IOException ex) {
+                throw new RuntimeException(ex);
+            }
+            return this;
+        }
 
     }