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-7287] Minor optimizations
Date Tue, 21 Mar 2017 13:10:54 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.1.x-fixes 5f6a422ba -> e6ffe48b9


[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/e6ffe48b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/e6ffe48b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/e6ffe48b

Branch: refs/heads/3.1.x-fixes
Commit: e6ffe48b9651e342997e85ebc65e889eee10d2b0
Parents: 5f6a422
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Tue Mar 21 13:00:11 2017 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Tue Mar 21 13:10:34 2017 +0000

----------------------------------------------------------------------
 .../json/basic/JsonMapObjectReaderWriter.java   | 93 ++++++++++++--------
 1 file changed, 58 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/e6ffe48b/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 584dcd3..df4070e 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);
@@ -222,8 +229,9 @@ 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("\"")) {
+
+        String valueStr = value.toString().trim();
+        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 +248,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 +273,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 +299,7 @@ public class JsonMapObjectReaderWriter {
     }
     private interface Output {
         Output append(String str);
+        Output append(char ch);
     }
     private class StringBuilderOutput implements Output {
         private StringBuilder sb;
@@ -300,7 +311,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 {
         private OutputStream os;
@@ -319,7 +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;
+        }
     }
     
 }


Mime
View raw message