lib/core: Renamed `Text::bytelen` to `Text::byte_length`
[nit.git] / lib / json / static.nit
index 15a4355..dd1cc85 100644 (file)
@@ -31,6 +31,9 @@ private import json_lexer
 interface Jsonable
        # Encode `self` in JSON.
        #
+       # This is a recursive method which can be refined by any subclasses.
+       # To write any `Serializable` object to JSON, see `serialize_to_json`.
+       #
        # SEE: `append_json`
        fun to_json: String is abstract
 
@@ -136,9 +139,10 @@ redef class Text
        #     assert "\\nEscape\\t\\n".json_to_nit_string == "\nEscape\t\n"
        #     assert "\\u0041zu\\uD800\\uDFD3".json_to_nit_string == "Azu𐏓"
        protected fun json_to_nit_string: String do
-               var res = new FlatBuffer.with_capacity(bytelen)
+               var res = new FlatBuffer.with_capacity(byte_length)
                var i = 0
-               while i < self.length do
+               var ln = self.length
+               while i < ln do
                        var char = self[i]
                        if char == '\\' then
                                i += 1
@@ -154,21 +158,19 @@ redef class Text
                                else if char == 't' then
                                        char = '\t'
                                else if char == 'u' then
-                                       var code = substring(i + 1, 4)
-                                       var hx = code.to_hex
-                                       if hx >= 0xD800 and hx <= 0xDFFF then
-                                               var lostr = substring(i + 7, 4)
-                                               if lostr.length < 4 then
-                                                       hx = 0xFFFD
+                                       var u16_esc = from_utf16_digit(i + 1)
+                                       char = u16_esc.code_point
+                                       if char.is_surrogate and i + 10 < ln then
+                                               if self[i + 5] == '\\' and self[i + 6] == 'u' then
+                                                       u16_esc <<= 16
+                                                       u16_esc += from_utf16_digit(i + 7)
+                                                       char = u16_esc.from_utf16_surr.code_point
+                                                       i += 6
                                                else
-                                                       hx <<= 16
-                                                       hx += lostr.to_hex
-                                                       hx = hx.from_utf16_surr
+                                                       char = 0xFFFD.code_point
                                                end
-                                               i += 6
                                        end
                                        i += 4
-                                       char = hx.code_point
                                end
                                # `"`, `/` or `\` => Keep `char` as-is.
                        end
@@ -186,7 +188,7 @@ redef class Text
        #     "\"\\t\\\"http://example.com\\\"\\r\\n\\u0000\\\\\""
        # ~~~
        redef fun to_json do
-               var b = new FlatBuffer.with_capacity(bytelen)
+               var b = new FlatBuffer.with_capacity(byte_length)
                append_json(b)
                return b.to_s
        end
@@ -440,6 +442,11 @@ redef class JsonParseError
                                "\"position\":{position.to_json}," +
                                "\"message\":{message.to_json}\}"
        end
+
+       redef fun pretty_json_visit(buf, indents) do
+               buf.clear
+               buf.append(to_json)
+       end
 end
 
 redef class Position