X-Git-Url: http://nitlanguage.org?ds=sidebyside diff --git a/lib/json/static.nit b/lib/json/static.nit index 15a4355..e333b85 100644 --- a/lib/json/static.nit +++ b/lib/json/static.nit @@ -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 @@ -138,7 +141,8 @@ redef class Text protected fun json_to_nit_string: String do var res = new FlatBuffer.with_capacity(bytelen) 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 @@ -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