diff --git a/src/Rosetta/codecs/text/Json/Decode.luau b/src/Rosetta/codecs/text/Json/Decode.luau index 4a7ceb1..fc86a38 100644 --- a/src/Rosetta/codecs/text/Json/Decode.luau +++ b/src/Rosetta/codecs/text/Json/Decode.luau @@ -29,6 +29,8 @@ local ESC: { [number]: string } = { [116] = "\t", } +local WS: { [number]: boolean } = { [32] = true, [9] = true, [10] = true, [13] = true } + local utf8char = Text.utf8char Decode.utf8char = utf8char @@ -164,7 +166,9 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) local value: any while true do - i = match(s, "^[ \t\n\r]*()", i) :: any + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end local c = byte(s, i) local haveValue = true if c == 123 then -- { @@ -172,7 +176,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) if depth > maxDepth then return nil, "nested deeper than " .. maxDepth, i - 1 end - i = match(s, "^[ \t\n\r]*()", i + 1) :: any + i = i + 1 + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end local t = {} c = byte(s, i) if c == 125 then @@ -184,7 +191,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) if k == nil then return nil, ni, at end - i = match(s, "^[ \t\n\r]*()", ni) :: any + i = ni + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end if byte(s, i) ~= 58 then return nil, if i > n then "truncated object" else "expected ':'", i - 1 end @@ -200,7 +210,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) if depth > maxDepth then return nil, "nested deeper than " .. maxDepth, i - 1 end - i = match(s, "^[ \t\n\r]*()", i + 1) :: any + i = i + 1 + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end if byte(s, i) == 93 then i += 1 depth -= 1 @@ -256,10 +269,15 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) t[cnt] = value stackK[depth] = cnt end - i = match(s, "^[ \t\n\r]*()", i) :: any + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end c = byte(s, i) if c == 44 then -- , - i = match(s, "^[ \t\n\r]*()", i + 1) :: any + i = i + 1 + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end if ord then if byte(s, i) ~= 34 then return nil, if i > n then "truncated object" else "expected a key", i - 1 @@ -268,7 +286,10 @@ function Decode.value(s: string, i: number, opts: Options?): (any, any, number?) if k == nil then return nil, ni, at end - i = match(s, "^[ \t\n\r]*()", ni) :: any + i = ni + if WS[byte(s, i) or 0] then + i = match(s, "^[ \t\n\r]*()", i) :: any + end if byte(s, i) ~= 58 then return nil, if i > n then "truncated object" else "expected ':'", i - 1 end diff --git a/src/Rosetta/codecs/text/Json/Encode.luau b/src/Rosetta/codecs/text/Json/Encode.luau index e8946ea..f166472 100644 --- a/src/Rosetta/codecs/text/Json/Encode.luau +++ b/src/Rosetta/codecs/text/Json/Encode.luau @@ -140,18 +140,23 @@ type State = { maxDepth: number, null: any, err: string?, + nls: { string }, -- "\n" .. indent * (i - 1) } -local function put(st: State, s: string) - local n = st.n + 1 - st.out[n] = s - st.n = n +local function newline(st: State, depth: number): string + local r = "\n" .. string.rep(st.indent :: string, depth) + st.nls[depth + 1] = r + return r end local function value(st: State, v: any, depth: number): boolean local tv = type(v) + local out = st.out + local n: number if tv == "string" then - put(st, quote(v, st.ascii, st.slash)) + n = st.n + 1 + out[n] = quote(v, st.ascii, st.slash) + st.n = n elseif tv == "number" then local s = number(v) if not s then @@ -161,14 +166,22 @@ local function value(st: State, v: any, depth: number): boolean end s = "null" end - put(st, s) + n = st.n + 1 + out[n] = s + st.n = n elseif tv == "boolean" then - put(st, if v then "true" else "false") + n = st.n + 1 + out[n] = if v then "true" else "false" + st.n = n elseif v == nil or v == st.null or v == Types.null then - put(st, "null") + n = st.n + 1 + out[n] = "null" + st.n = n elseif tv == "table" then if Types.Date.is(v) then - put(st, '"' .. tostring(v) .. '"') + n = st.n + 1 + out[n] = '"' .. tostring(v) .. '"' + st.n = n return true end if depth >= st.maxDepth then @@ -178,31 +191,44 @@ local function value(st: State, v: any, depth: number): boolean local ind = st.indent local nl, nl2 = "", "" if ind then - nl = "\n" .. string.rep(ind, depth + 1) - nl2 = "\n" .. string.rep(ind, depth) + local nls = st.nls + nl = nls[depth + 2] or newline(st, depth + 1) + nl2 = nls[depth + 1] or newline(st, depth) end if kind(v, st.emptyObject) == "array" then - local n = #v - if n == 0 then - put(st, "[]") + local len = #v + if len == 0 then + n = st.n + 1 + out[n] = "[]" + st.n = n return true end - put(st, "[") - for i = 1, n do + n = st.n + 1 + out[n] = "[" + st.n = n + for i = 1, len do if i > 1 then - put(st, ",") + n = st.n + 1 + out[n] = "," + st.n = n end if ind then - put(st, nl) + n = st.n + 1 + out[n] = nl + st.n = n end if not value(st, v[i], depth + 1) then return false end end if ind then - put(st, nl2) + n = st.n + 1 + out[n] = nl2 + st.n = n end - put(st, "]") + n = st.n + 1 + out[n] = "]" + st.n = n else local keys: { any } if Types.isObject(v) then @@ -222,17 +248,25 @@ local function value(st: State, v: any, depth: number): boolean end end if #keys == 0 then - put(st, "{}") + n = st.n + 1 + out[n] = "{}" + st.n = n return true end - put(st, "{") + n = st.n + 1 + out[n] = "{" + st.n = n local sep = if ind then ": " else ":" for i, k in keys do if i > 1 then - put(st, ",") + n = st.n + 1 + out[n] = "," + st.n = n end if ind then - put(st, nl) + n = st.n + 1 + out[n] = nl + st.n = n end local ks: string if type(k) == "string" then @@ -243,16 +277,24 @@ local function value(st: State, v: any, depth: number): boolean st.err = "object key of type " .. type(k) return false end - put(st, quote(ks, st.ascii, st.slash)) - put(st, sep) + n = st.n + 1 + out[n] = quote(ks, st.ascii, st.slash) + st.n = n + n = st.n + 1 + out[n] = sep + st.n = n if not value(st, v[k], depth + 1) then return false end end if ind then - put(st, nl2) + n = st.n + 1 + out[n] = nl2 + st.n = n end - put(st, "}") + n = st.n + 1 + out[n] = "}" + st.n = n end elseif tv == "buffer" then st.err = "cannot encode a buffer (encode it as base64 or an array first)" @@ -284,6 +326,7 @@ local function state(opts: Options?, nullv: any?): State maxDepth = o.maxDepth or 1000, null = nullv, err = nil, + nls = {}, } end Encode.state = state