|
| 1 | +-- |
| 2 | +-- json.lua |
| 3 | +-- |
| 4 | +-- Copyright (c) 2020 rxi |
| 5 | +-- |
| 6 | +-- Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +-- this software and associated documentation files (the "Software"), to deal in |
| 8 | +-- the Software without restriction, including without limitation the rights to |
| 9 | +-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | +-- of the Software, and to permit persons to whom the Software is furnished to do |
| 11 | +-- so, subject to the following conditions: |
| 12 | +-- |
| 13 | +-- The above copyright notice and this permission notice shall be included in all |
| 14 | +-- copies or substantial portions of the Software. |
| 15 | +-- |
| 16 | +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +-- SOFTWARE. |
| 23 | +-- |
| 24 | + |
| 25 | +local json = { _version = "0.1.2" } |
| 26 | + |
| 27 | +------------------------------------------------------------------------------- |
| 28 | +-- Encode |
| 29 | +------------------------------------------------------------------------------- |
| 30 | + |
| 31 | +local encode |
| 32 | + |
| 33 | +local escape_char_map = { |
| 34 | + ["\\"] = "\\", |
| 35 | + ['"'] = '"', |
| 36 | + ["\b"] = "b", |
| 37 | + ["\f"] = "f", |
| 38 | + ["\n"] = "n", |
| 39 | + ["\r"] = "r", |
| 40 | + ["\t"] = "t", |
| 41 | +} |
| 42 | + |
| 43 | +local escape_char_map_inv = { ["/"] = "/" } |
| 44 | +for k, v in pairs(escape_char_map) do |
| 45 | + escape_char_map_inv[v] = k |
| 46 | +end |
| 47 | + |
| 48 | +local function escape_char(c) |
| 49 | + return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte())) |
| 50 | +end |
| 51 | + |
| 52 | +local function encode_nil(val) |
| 53 | + return "null" |
| 54 | +end |
| 55 | + |
| 56 | +local function encode_table(val, stack) |
| 57 | + local res = {} |
| 58 | + stack = stack or {} |
| 59 | + |
| 60 | + -- Circular reference? |
| 61 | + if stack[val] then |
| 62 | + error("circular reference") |
| 63 | + end |
| 64 | + |
| 65 | + stack[val] = true |
| 66 | + |
| 67 | + if rawget(val, 1) ~= nil or next(val) == nil then |
| 68 | + -- Treat as array -- check keys are valid and it is not sparse |
| 69 | + local n = 0 |
| 70 | + for k in pairs(val) do |
| 71 | + if type(k) ~= "number" then |
| 72 | + error("invalid table: mixed or invalid key types") |
| 73 | + end |
| 74 | + n = n + 1 |
| 75 | + end |
| 76 | + if n ~= #val then |
| 77 | + error("invalid table: sparse array") |
| 78 | + end |
| 79 | + -- Encode |
| 80 | + for i, v in ipairs(val) do |
| 81 | + table.insert(res, encode(v, stack)) |
| 82 | + end |
| 83 | + stack[val] = nil |
| 84 | + return "[" .. table.concat(res, ",") .. "]" |
| 85 | + else |
| 86 | + -- Treat as an object |
| 87 | + for k, v in pairs(val) do |
| 88 | + if type(k) ~= "string" then |
| 89 | + error("invalid table: mixed or invalid key types") |
| 90 | + end |
| 91 | + table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) |
| 92 | + end |
| 93 | + stack[val] = nil |
| 94 | + return "{" .. table.concat(res, ",") .. "}" |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +local function encode_string(val) |
| 99 | + return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"' |
| 100 | +end |
| 101 | + |
| 102 | +local function encode_number(val) |
| 103 | + -- Check for NaN, -inf and inf |
| 104 | + if val ~= val or val <= -math.huge or val >= math.huge then |
| 105 | + error("unexpected number value '" .. tostring(val) .. "'") |
| 106 | + end |
| 107 | + return string.format("%.14g", val) |
| 108 | +end |
| 109 | + |
| 110 | +local type_func_map = { |
| 111 | + ["nil"] = encode_nil, |
| 112 | + ["table"] = encode_table, |
| 113 | + ["string"] = encode_string, |
| 114 | + ["number"] = encode_number, |
| 115 | + ["boolean"] = tostring, |
| 116 | +} |
| 117 | + |
| 118 | +encode = function(val, stack) |
| 119 | + local t = type(val) |
| 120 | + local f = type_func_map[t] |
| 121 | + if f then |
| 122 | + return f(val, stack) |
| 123 | + end |
| 124 | + error("unexpected type '" .. t .. "'") |
| 125 | +end |
| 126 | + |
| 127 | +function json.encode(val) |
| 128 | + return (encode(val)) |
| 129 | +end |
| 130 | + |
| 131 | +------------------------------------------------------------------------------- |
| 132 | +-- Decode |
| 133 | +------------------------------------------------------------------------------- |
| 134 | + |
| 135 | +local parse |
| 136 | + |
| 137 | +local function create_set(...) |
| 138 | + local res = {} |
| 139 | + for i = 1, select("#", ...) do |
| 140 | + res[select(i, ...)] = true |
| 141 | + end |
| 142 | + return res |
| 143 | +end |
| 144 | + |
| 145 | +local space_chars = create_set(" ", "\t", "\r", "\n") |
| 146 | +local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") |
| 147 | +local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") |
| 148 | +local literals = create_set("true", "false", "null") |
| 149 | + |
| 150 | +local literal_map = { |
| 151 | + ["true"] = true, |
| 152 | + ["false"] = false, |
| 153 | + ["null"] = nil, |
| 154 | +} |
| 155 | + |
| 156 | +local function next_char(str, idx, set, negate) |
| 157 | + for i = idx, #str do |
| 158 | + if set[str:sub(i, i)] ~= negate then |
| 159 | + return i |
| 160 | + end |
| 161 | + end |
| 162 | + return #str + 1 |
| 163 | +end |
| 164 | + |
| 165 | +local function decode_error(str, idx, msg) |
| 166 | + local line_count = 1 |
| 167 | + local col_count = 1 |
| 168 | + for i = 1, idx - 1 do |
| 169 | + col_count = col_count + 1 |
| 170 | + if str:sub(i, i) == "\n" then |
| 171 | + line_count = line_count + 1 |
| 172 | + col_count = 1 |
| 173 | + end |
| 174 | + end |
| 175 | + error(string.format("%s at line %d col %d", msg, line_count, col_count)) |
| 176 | +end |
| 177 | + |
| 178 | +local function codepoint_to_utf8(n) |
| 179 | + -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa |
| 180 | + local f = math.floor |
| 181 | + if n <= 0x7f then |
| 182 | + return string.char(n) |
| 183 | + elseif n <= 0x7ff then |
| 184 | + return string.char(f(n / 64) + 192, n % 64 + 128) |
| 185 | + elseif n <= 0xffff then |
| 186 | + return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) |
| 187 | + elseif n <= 0x10ffff then |
| 188 | + return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, f(n % 4096 / 64) + 128, n % 64 + 128) |
| 189 | + end |
| 190 | + error(string.format("invalid unicode codepoint '%x'", n)) |
| 191 | +end |
| 192 | + |
| 193 | +local function parse_unicode_escape(s) |
| 194 | + local n1 = tonumber(s:sub(1, 4), 16) |
| 195 | + local n2 = tonumber(s:sub(7, 10), 16) |
| 196 | + -- Surrogate pair? |
| 197 | + if n2 then |
| 198 | + return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) |
| 199 | + else |
| 200 | + return codepoint_to_utf8(n1) |
| 201 | + end |
| 202 | +end |
| 203 | + |
| 204 | +local function parse_string(str, i) |
| 205 | + local res = "" |
| 206 | + local j = i + 1 |
| 207 | + local k = j |
| 208 | + |
| 209 | + while j <= #str do |
| 210 | + local x = str:byte(j) |
| 211 | + |
| 212 | + if x < 32 then |
| 213 | + decode_error(str, j, "control character in string") |
| 214 | + elseif x == 92 then -- `\`: Escape |
| 215 | + res = res .. str:sub(k, j - 1) |
| 216 | + j = j + 1 |
| 217 | + local c = str:sub(j, j) |
| 218 | + if c == "u" then |
| 219 | + local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1) |
| 220 | + or str:match("^%x%x%x%x", j + 1) |
| 221 | + or decode_error(str, j - 1, "invalid unicode escape in string") |
| 222 | + res = res .. parse_unicode_escape(hex) |
| 223 | + j = j + #hex |
| 224 | + else |
| 225 | + if not escape_chars[c] then |
| 226 | + decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string") |
| 227 | + end |
| 228 | + res = res .. escape_char_map_inv[c] |
| 229 | + end |
| 230 | + k = j + 1 |
| 231 | + elseif x == 34 then -- `"`: End of string |
| 232 | + res = res .. str:sub(k, j - 1) |
| 233 | + return res, j + 1 |
| 234 | + end |
| 235 | + |
| 236 | + j = j + 1 |
| 237 | + end |
| 238 | + |
| 239 | + decode_error(str, i, "expected closing quote for string") |
| 240 | +end |
| 241 | + |
| 242 | +local function parse_number(str, i) |
| 243 | + local x = next_char(str, i, delim_chars) |
| 244 | + local s = str:sub(i, x - 1) |
| 245 | + local n = tonumber(s) |
| 246 | + if not n then |
| 247 | + decode_error(str, i, "invalid number '" .. s .. "'") |
| 248 | + end |
| 249 | + return n, x |
| 250 | +end |
| 251 | + |
| 252 | +local function parse_literal(str, i) |
| 253 | + local x = next_char(str, i, delim_chars) |
| 254 | + local word = str:sub(i, x - 1) |
| 255 | + if not literals[word] then |
| 256 | + decode_error(str, i, "invalid literal '" .. word .. "'") |
| 257 | + end |
| 258 | + return literal_map[word], x |
| 259 | +end |
| 260 | + |
| 261 | +local function parse_array(str, i) |
| 262 | + local res = {} |
| 263 | + local n = 1 |
| 264 | + i = i + 1 |
| 265 | + while 1 do |
| 266 | + local x |
| 267 | + i = next_char(str, i, space_chars, true) |
| 268 | + -- Empty / end of array? |
| 269 | + if str:sub(i, i) == "]" then |
| 270 | + i = i + 1 |
| 271 | + break |
| 272 | + end |
| 273 | + -- Read token |
| 274 | + x, i = parse(str, i) |
| 275 | + res[n] = x |
| 276 | + n = n + 1 |
| 277 | + -- Next token |
| 278 | + i = next_char(str, i, space_chars, true) |
| 279 | + local chr = str:sub(i, i) |
| 280 | + i = i + 1 |
| 281 | + if chr == "]" then |
| 282 | + break |
| 283 | + end |
| 284 | + if chr ~= "," then |
| 285 | + decode_error(str, i, "expected ']' or ','") |
| 286 | + end |
| 287 | + end |
| 288 | + return res, i |
| 289 | +end |
| 290 | + |
| 291 | +local function parse_object(str, i) |
| 292 | + local res = {} |
| 293 | + i = i + 1 |
| 294 | + while 1 do |
| 295 | + local key, val |
| 296 | + i = next_char(str, i, space_chars, true) |
| 297 | + -- Empty / end of object? |
| 298 | + if str:sub(i, i) == "}" then |
| 299 | + i = i + 1 |
| 300 | + break |
| 301 | + end |
| 302 | + -- Read key |
| 303 | + if str:sub(i, i) ~= '"' then |
| 304 | + decode_error(str, i, "expected string for key") |
| 305 | + end |
| 306 | + key, i = parse(str, i) |
| 307 | + -- Read ':' delimiter |
| 308 | + i = next_char(str, i, space_chars, true) |
| 309 | + if str:sub(i, i) ~= ":" then |
| 310 | + decode_error(str, i, "expected ':' after key") |
| 311 | + end |
| 312 | + i = next_char(str, i + 1, space_chars, true) |
| 313 | + -- Read value |
| 314 | + val, i = parse(str, i) |
| 315 | + -- Set |
| 316 | + res[key] = val |
| 317 | + -- Next token |
| 318 | + i = next_char(str, i, space_chars, true) |
| 319 | + local chr = str:sub(i, i) |
| 320 | + i = i + 1 |
| 321 | + if chr == "}" then |
| 322 | + break |
| 323 | + end |
| 324 | + if chr ~= "," then |
| 325 | + decode_error(str, i, "expected '}' or ','") |
| 326 | + end |
| 327 | + end |
| 328 | + return res, i |
| 329 | +end |
| 330 | + |
| 331 | +local char_func_map = { |
| 332 | + ['"'] = parse_string, |
| 333 | + ["0"] = parse_number, |
| 334 | + ["1"] = parse_number, |
| 335 | + ["2"] = parse_number, |
| 336 | + ["3"] = parse_number, |
| 337 | + ["4"] = parse_number, |
| 338 | + ["5"] = parse_number, |
| 339 | + ["6"] = parse_number, |
| 340 | + ["7"] = parse_number, |
| 341 | + ["8"] = parse_number, |
| 342 | + ["9"] = parse_number, |
| 343 | + ["-"] = parse_number, |
| 344 | + ["t"] = parse_literal, |
| 345 | + ["f"] = parse_literal, |
| 346 | + ["n"] = parse_literal, |
| 347 | + ["["] = parse_array, |
| 348 | + ["{"] = parse_object, |
| 349 | +} |
| 350 | + |
| 351 | +parse = function(str, idx) |
| 352 | + local chr = str:sub(idx, idx) |
| 353 | + local f = char_func_map[chr] |
| 354 | + if f then |
| 355 | + return f(str, idx) |
| 356 | + end |
| 357 | + decode_error(str, idx, "unexpected character '" .. chr .. "'") |
| 358 | +end |
| 359 | + |
| 360 | +function json.decode(str) |
| 361 | + if type(str) ~= "string" then |
| 362 | + error("expected argument of type string, got " .. type(str)) |
| 363 | + end |
| 364 | + local res, idx = parse(str, next_char(str, 1, space_chars, true)) |
| 365 | + idx = next_char(str, idx, space_chars, true) |
| 366 | + if idx <= #str then |
| 367 | + decode_error(str, idx, "trailing garbage") |
| 368 | + end |
| 369 | + return res |
| 370 | +end |
| 371 | + |
| 372 | +return json |
0 commit comments