Author: rwhitcomb
Date: Thu Apr 15 16:42:26 2021
New Revision: 1888797

URL: http://svn.apache.org/viewvc?rev=1888797&view=rev
Log:
Implement extended Unicode escapes in JSON parsing: \u{xxxxx}.

Modified:
    pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
    pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java
    pivot/trunk/core/test/org/apache/pivot/json/test/map.json

Modified: pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java?rev=1888797&r1=1888796&r2=1888797&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/json/JSONSerializer.java Thu Apr 15 
16:42:26 2021
@@ -365,13 +365,31 @@ public class JSONSerializer implements S
                         c = '\t';
                     } else if (c == 'u') {
                         StringBuilder unicodeBuilder = new StringBuilder();
-                        while (unicodeBuilder.length() < 4) {
+                        c = reader.read();
+                        if (c == '{') {
                             c = reader.read();
+                            while (c != '}') {
+                                unicodeBuilder.append((char) c);
+                                c = reader.read();
+                            }
+                            
+                        } else {
                             unicodeBuilder.append((char) c);
+                            while (unicodeBuilder.length() < 4) {
+                                c = reader.read();
+                                unicodeBuilder.append((char) c);
+                            }
                         }
 
                         String unicode = unicodeBuilder.toString();
-                        c = (char) Integer.parseInt(unicode, 16);
+                        int cp = Integer.parseInt(unicode, 16);
+                        if (cp <= 0xFFFF) {
+                            c = cp;
+                        } else {
+                            stringBuilder.appendCodePoint(cp);
+                            c = reader.read();
+                            continue;
+                        }
                     } else {
                         if (!(c == '\\' || c == '/' || c == '\"' || c == '\'' 
|| c == t)) {
                             throw new SerializationException(

Modified: 
pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java?rev=1888797&r1=1888796&r2=1888797&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java 
(original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/JSONSerializerTest.java 
Thu Apr 15 16:42:26 2021
@@ -151,6 +151,8 @@ public class JSONSerializerTest {
         assertEquals(JSON.get(o2, "k[2].c"), "300");
         assertEquals((Integer) JSON.get(o2, "j"), (Integer) 200);
         assertEquals(JSON.get(o2, "n"), "This is a \"test\" of the 'quoting' 
in \\JSON\\");
+        assertEquals(JSON.get(o2, "emoji"), "Trying out an emoji unicode value 
\uD83D\uDC4F!");
+        assertEquals(JSON.get(o2, "uni"), "Regular unicode escape \u2318.");
 
         assertTrue(o1.equals(o2));
 

Modified: pivot/trunk/core/test/org/apache/pivot/json/test/map.json
URL: 
http://svn.apache.org/viewvc/pivot/trunk/core/test/org/apache/pivot/json/test/map.json?rev=1888797&r1=1888796&r2=1888797&view=diff
==============================================================================
--- pivot/trunk/core/test/org/apache/pivot/json/test/map.json (original)
+++ pivot/trunk/core/test/org/apache/pivot/json/test/map.json Thu Apr 15 
16:42:26 2021
@@ -37,5 +37,7 @@
         {a:1${ZERO}${ZERO}, b:${TWO_HUNDRED}, c:"${THREE_HUNDRED}"}
     ],
     m: "Hello\r\n\tWorld!",
-    n: "This is a \"test\" of the \'quoting\' in \\JSON\\"
+    n: "This is a \"test\" of the \'quoting\' in \\JSON\\",
+    emoji: 'Trying out an emoji unicode value \u{1f44f}!',
+    uni: 'Regular unicode escape \u2318.'
 }


Reply via email to