json: Parse all standard escape sequences.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Mon, 17 Nov 2014 14:46:05 +0000 (09:46 -0500)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Mon, 17 Nov 2014 14:46:05 +0000 (09:46 -0500)
Only escaped UTF-16 wydes above 127 are not supported yet.

Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

lib/json/static.nit
tests/sav/test_json_static.res
tests/test_json_static.nit

index 51800fc..3743da3 100644 (file)
@@ -1,6 +1,8 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+# Copyright 2014 Alexandre Terrasa <alexandre@moz-concept.com>
+# Copyright 2014 Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -55,11 +57,41 @@ redef class Nvalue_null
 end
 
 redef class Nstring
-       # FIXME support \n, etc.
-       fun to_nit_string: String do return text.substring(1, text.length-2).
-               replace("\\\\", "\\").replace("\\\"", "\"").replace("\\b", "\b").
-               replace("\\/", "/").replace("\\n", "\n").replace("\\r", "\r").
-               replace("\\t", "\t")
+       fun to_nit_string: String do
+               var res = new FlatBuffer
+               var i = 1
+               while i < text.length - 1 do
+                       var char = text[i]
+                       if char == '\\' then
+                               i += 1
+                               char = text[i]
+                               if char == 'b' then
+                                       char = 0x08.ascii
+                               else if char == 'f' then
+                                       char = 0x0C.ascii
+                               else if char == 'n' then
+                                       char = '\n'
+                               else if char == 'r' then
+                                       char = '\r'
+                               else if char == 't' then
+                                       char = '\t'
+                               else if char == 'u' then
+                                       var code = text.substring(i + 1, 4).to_hex
+                                       # TODO UTF-16 escaping is not supported yet.
+                                       if code >= 128 then
+                                               char = '?'
+                                       else
+                                               char = code.ascii
+                                       end
+                                       i += 4
+                               end
+                               # `"`, `/` or `\` => Keep `char` as-is.
+                       end
+                       res.add char
+                       i += 1
+               end
+               return res.write_to_string
+       end
 end
 
 redef class Nvalue_object
index 0a122d4..22ab94a 100644 (file)
Binary files a/tests/sav/test_json_static.res and b/tests/sav/test_json_static.res differ
index b510725..efc65d2 100644 (file)
@@ -1,6 +1,7 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+# Copyright 2014 Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -22,8 +23,10 @@ end
 
 var a = "\{\"__kind\": \"obj\", \"__id\": 0, \"__class\": \"C\", \"a\": \{\"__kind\": \"obj\", \"__id\": 1, \"__class\": \"A\", \"b\": true, \"c\": \"a\", \"f\": 0.123, \"i\": 1234, \"s\": \"asdf\", \"n\": null, \"array\": [88, \"hello\", null]\}, \"b\": \{\"__kind\": \"obj\", \"__id\": 2, \"__class\": \"B\", \"b\": false, \"c\": \"b\", \"f\": 123.123, \"i\": 2345, \"s\": \"hjkl\", \"n\": null, \"array\": [88, \"hello\", null], \"ii\": 1111, \"ss\": \"qwer\"\}, \"aa\": \{\"__kind\": \"ref\", \"__id\": 1\}\}"
 var b = "\{\"__kind\": \"obj\", \"__id\": 0, \"__class\": \"A\", \"b\": true, \"c\": \"a\", \"f\": 0.123, \"i\": 1234, \"s\": \"asdf\", \"n\": null, \"array\": [88, \"hello\", null]\}"
+var c = "\{\"foo\":\"bar\\\"\\\\\\/\\b\\f\\n\\r\\t\\u0020\\u0000\"\}"
+var d = "\{ \"face with tears of joy\" : \"\\uD83D\\uDE02\" \}"
 
-for s in [a, b] do
+for s in [a, b, c, d] do
        var obj = s.json_to_nit_object
        print "# Json: {s}"
        print "# Nit: {obj or else "<null>"}"