jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1350206 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java main/java/org/apache/jackrabbit/oak/util/JsonBuilder.java test/java/org/apache/jackrabbit/oak/util/JsonBuilderTest.java
Date Thu, 14 Jun 2012 12:10:11 GMT
Author: mduerig
Date: Thu Jun 14 12:10:11 2012
New Revision: 1350206

URL: http://svn.apache.org/viewvc?rev=1350206&view=rev
Log:
OAK-139: Remove JsonBuilder

Removed:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/util/JsonBuilder.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/util/JsonBuilderTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java?rev=1350206&r1=1350205&r2=1350206&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/CoreValueMapper.java
Thu Jun 14 12:10:11 2012
@@ -16,17 +16,17 @@
  */
 package org.apache.jackrabbit.oak.kernel;
 
-import org.apache.jackrabbit.oak.util.JsonBuilder;
-import org.apache.jackrabbit.mk.json.JsopReader;
-import org.apache.jackrabbit.oak.api.CoreValue;
-import org.apache.jackrabbit.oak.api.CoreValueFactory;
-
-import javax.jcr.PropertyType;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.jcr.PropertyType;
+
+import org.apache.jackrabbit.mk.json.JsopReader;
+import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.api.CoreValueFactory;
+
 /**
  * CoreValueUtil provides methods to convert {@code CoreValue}s to the JSON
  * representation passed to MicroKernel and vice versa.
@@ -57,25 +57,22 @@ public class CoreValueMapper {
      *
      * @param value The core value to be converted.
      * @return The encoded JSON string.
-     * @see JsonBuilder#encode(String)
-     * @see JsonBuilder#encode(long)
-     * @see JsonBuilder#encode(long)
      */
     public static String toJsonValue(CoreValue value) {
         String jsonString;
         switch (value.getType()) {
             case PropertyType.BOOLEAN:
-                jsonString = JsonBuilder.encode(value.getBoolean());
+                jsonString = Boolean.toString(value.getBoolean());
                 break;
             case PropertyType.LONG:
-                jsonString = JsonBuilder.encode(value.getLong());
+                jsonString = Long.toString(value.getLong());
                 break;
             case PropertyType.STRING:
                 String str = value.getString();
                 if (startsWithHint(str)) {
                     jsonString = buildJsonStringWithHint(value);
                 } else {
-                    jsonString = JsonBuilder.encode(value.getString());
+                    jsonString = jsonEncode(value.getString());
                 }
                 break;
             default:
@@ -171,7 +168,7 @@ public class CoreValueMapper {
         sb.append(TYPE2HINT.get(value.getType()));
         sb.append(':');
         sb.append(value.getString());
-        return JsonBuilder.encode(sb.toString());
+        return jsonEncode(sb.toString());
     }
 
     /**
@@ -187,4 +184,59 @@ public class CoreValueMapper {
         return jsonString.length() >= 4 && jsonString.charAt(3) == ':';
     }
 
+    /**
+     * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters
+     * (U+0000 through U+001F) and surround with double quotes.
+     */
+    private static String jsonEncode(String value) {
+        if (value == null) {
+            return null;
+        }
+
+        StringBuilder sb = new StringBuilder("\"");
+        for (int i = 0; i < value.length(); i++) {
+            char ch = value.charAt(i);
+            switch (ch) {
+                case '"':
+                    sb.append("\\\"");
+                    break;
+                case '\\':
+                    sb.append("\\\\");
+                    break;
+                case '\b':
+                    sb.append("\\b");
+                    break;
+                case '\f':
+                    sb.append("\\f");
+                    break;
+                case '\n':
+                    sb.append("\\n");
+                    break;
+                case '\r':
+                    sb.append("\\r");
+                    break;
+                case '\t':
+                    sb.append("\\t");
+                    break;
+                default:
+                    //Reference: http://www.unicode.org/versions/Unicode5.1.0/
+                    if (ch >= '\u0000' && ch <= '\u001F' ||
+                            ch >= '\u007F' && ch <= '\u009F' ||
+                            ch >= '\u2000' && ch <= '\u20FF') {
+
+                        String ss = Integer.toHexString(ch);
+                        sb.append("\\u");
+                        for (int k = 0; k < 4 - ss.length(); k++) {
+                            sb.append('0');
+                        }
+                        sb.append(ss.toUpperCase());
+                    } else {
+                        sb.append(ch);
+                    }
+            }
+        }
+
+        return sb.append('"').toString();
+    }
+
 }
\ No newline at end of file



Mime
View raw message