Serge Bazanski | 18c1a26 | 2022-07-07 14:24:53 +0200 | [diff] [blame] | 1 | -- |
| 2 | -- json.lua |
| 3 | -- |
| 4 | -- Copyright (c) 2015 rxi |
| 5 | -- |
| 6 | -- This library is free software; you can redistribute it and/or modify it |
| 7 | -- under the terms of the MIT license. See LICENSE for details. |
| 8 | -- |
| 9 | |
| 10 | local json = { _version = "0.1.0" } |
| 11 | |
| 12 | ------------------------------------------------------------------------------- |
| 13 | -- Encode |
| 14 | ------------------------------------------------------------------------------- |
| 15 | |
| 16 | local encode |
| 17 | |
| 18 | local escape_char_map = { |
| 19 | [ "\\" ] = "\\\\", |
| 20 | [ "\"" ] = "\\\"", |
| 21 | [ "\b" ] = "\\b", |
| 22 | [ "\f" ] = "\\f", |
| 23 | [ "\n" ] = "\\n", |
| 24 | [ "\r" ] = "\\r", |
| 25 | [ "\t" ] = "\\t", |
| 26 | } |
| 27 | |
| 28 | local escape_char_map_inv = { [ "\\/" ] = "/" } |
| 29 | for k, v in pairs(escape_char_map) do |
| 30 | escape_char_map_inv[v] = k |
| 31 | end |
| 32 | |
| 33 | |
| 34 | local function escape_char(c) |
| 35 | return escape_char_map[c] or string.format("\\u%04x", c:byte()) |
| 36 | end |
| 37 | |
| 38 | |
| 39 | local function encode_nil(val) |
| 40 | return "null" |
| 41 | end |
| 42 | |
| 43 | |
| 44 | local function encode_table(val, stack) |
| 45 | local res = {} |
| 46 | stack = stack or {} |
| 47 | |
| 48 | -- Circular reference? |
| 49 | if stack[val] then error("circular reference") end |
| 50 | |
| 51 | stack[val] = true |
| 52 | |
| 53 | if val[1] ~= nil or next(val) == nil then |
| 54 | -- Treat as array -- check keys are valid and it is not sparse |
| 55 | local n = 0 |
| 56 | for k in pairs(val) do |
| 57 | if type(k) ~= "number" then |
| 58 | error("invalid table: mixed or invalid key types") |
| 59 | end |
| 60 | n = n + 1 |
| 61 | end |
| 62 | if n ~= #val then |
| 63 | error("invalid table: sparse array") |
| 64 | end |
| 65 | -- Encode |
| 66 | for i, v in ipairs(val) do |
| 67 | table.insert(res, encode(v, stack)) |
| 68 | end |
| 69 | stack[val] = nil |
| 70 | return "[" .. table.concat(res, ",") .. "]" |
| 71 | |
| 72 | else |
| 73 | -- Treat as an object |
| 74 | for k, v in pairs(val) do |
| 75 | if type(k) ~= "string" then |
| 76 | error("invalid table: mixed or invalid key types") |
| 77 | end |
| 78 | table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) |
| 79 | end |
| 80 | stack[val] = nil |
| 81 | return "{" .. table.concat(res, ",") .. "}" |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | |
| 86 | local function encode_string(val) |
| 87 | return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"' |
| 88 | end |
| 89 | |
| 90 | |
| 91 | local function encode_number(val) |
| 92 | -- Check for NaN, -inf and inf |
| 93 | if val ~= val or val <= -math.huge or val >= math.huge then |
| 94 | error("unexpected number value '" .. tostring(val) .. "'") |
| 95 | end |
| 96 | return string.format("%.14g", val) |
| 97 | end |
| 98 | |
| 99 | |
| 100 | local type_func_map = { |
| 101 | [ "nil" ] = encode_nil, |
| 102 | [ "table" ] = encode_table, |
| 103 | [ "string" ] = encode_string, |
| 104 | [ "number" ] = encode_number, |
| 105 | [ "boolean" ] = tostring, |
| 106 | } |
| 107 | |
| 108 | |
| 109 | encode = function(val, stack) |
| 110 | local t = type(val) |
| 111 | local f = type_func_map[t] |
| 112 | if f then |
| 113 | return f(val, stack) |
| 114 | end |
| 115 | error("unexpected type '" .. t .. "'") |
| 116 | end |
| 117 | |
| 118 | |
| 119 | function json.encode(val) |
| 120 | return ( encode(val) ) |
| 121 | end |
| 122 | |
| 123 | |
| 124 | ------------------------------------------------------------------------------- |
| 125 | -- Decode |
| 126 | ------------------------------------------------------------------------------- |
| 127 | |
| 128 | local parse |
| 129 | |
| 130 | local function create_set(...) |
| 131 | local res = {} |
| 132 | for i = 1, select("#", ...) do |
| 133 | res[ select(i, ...) ] = true |
| 134 | end |
| 135 | return res |
| 136 | end |
| 137 | |
| 138 | local space_chars = create_set(" ", "\t", "\r", "\n") |
| 139 | local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") |
| 140 | local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") |
| 141 | local literals = create_set("true", "false", "null") |
| 142 | |
| 143 | local literal_map = { |
| 144 | [ "true" ] = true, |
| 145 | [ "false" ] = false, |
| 146 | [ "null" ] = nil, |
| 147 | } |
| 148 | |
| 149 | |
| 150 | local function next_char(str, idx, set, negate) |
| 151 | for i = idx, #str do |
| 152 | if set[str:sub(i, i)] ~= negate then |
| 153 | return i |
| 154 | end |
| 155 | end |
| 156 | return #str + 1 |
| 157 | end |
| 158 | |
| 159 | |
| 160 | local function decode_error(str, idx, msg) |
| 161 | local line_count = 1 |
| 162 | local col_count = 1 |
| 163 | for i = 1, idx - 1 do |
| 164 | col_count = col_count + 1 |
| 165 | if str:sub(i, i) == "\n" then |
| 166 | line_count = line_count + 1 |
| 167 | col_count = 1 |
| 168 | end |
| 169 | end |
| 170 | error( string.format("%s at line %d col %d", msg, line_count, col_count) ) |
| 171 | end |
| 172 | |
| 173 | |
| 174 | local function codepoint_to_utf8(n) |
| 175 | -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa |
| 176 | local f = math.floor |
| 177 | if n <= 0x7f then |
| 178 | return string.char(n) |
| 179 | elseif n <= 0x7ff then |
| 180 | return string.char(f(n / 64) + 192, n % 64 + 128) |
| 181 | elseif n <= 0xffff then |
| 182 | return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) |
| 183 | elseif n <= 0x10ffff then |
| 184 | return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, |
| 185 | f(n % 4096 / 64) + 128, n % 64 + 128) |
| 186 | end |
| 187 | error( string.format("invalid unicode codepoint '%x'", n) ) |
| 188 | end |
| 189 | |
| 190 | |
| 191 | local function parse_unicode_escape(s) |
| 192 | local n1 = tonumber( s:sub(3, 6), 16 ) |
| 193 | local n2 = tonumber( s:sub(9, 12), 16 ) |
| 194 | -- Surrogate pair? |
| 195 | if n2 then |
| 196 | return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) |
| 197 | else |
| 198 | return codepoint_to_utf8(n1) |
| 199 | end |
| 200 | end |
| 201 | |
| 202 | |
| 203 | local function parse_string(str, i) |
| 204 | local has_unicode_escape = false |
| 205 | local has_surrogate_escape = false |
| 206 | local has_escape = false |
| 207 | local last |
| 208 | for j = i + 1, #str do |
| 209 | local x = str:byte(j) |
| 210 | |
| 211 | if x < 32 then |
| 212 | decode_error(str, j, "control character in string") |
| 213 | end |
| 214 | |
| 215 | if last == 92 then -- "\\" (escape char) |
| 216 | if x == 117 then -- "u" (unicode escape sequence) |
| 217 | local hex = str:sub(j + 1, j + 5) |
| 218 | if not hex:find("%x%x%x%x") then |
| 219 | decode_error(str, j, "invalid unicode escape in string") |
| 220 | end |
| 221 | if hex:find("^[dD][89aAbB]") then |
| 222 | has_surrogate_escape = true |
| 223 | else |
| 224 | has_unicode_escape = true |
| 225 | end |
| 226 | else |
| 227 | local c = string.char(x) |
| 228 | if not escape_chars[c] then |
| 229 | decode_error(str, j, "invalid escape char '" .. c .. "' in string") |
| 230 | end |
| 231 | has_escape = true |
| 232 | end |
| 233 | last = nil |
| 234 | |
| 235 | elseif x == 34 then -- '"' (end of string) |
| 236 | local s = str:sub(i + 1, j - 1) |
| 237 | if has_surrogate_escape then |
| 238 | s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) |
| 239 | end |
| 240 | if has_unicode_escape then |
| 241 | s = s:gsub("\\u....", parse_unicode_escape) |
| 242 | end |
| 243 | if has_escape then |
| 244 | s = s:gsub("\\.", escape_char_map_inv) |
| 245 | end |
| 246 | return s, j + 1 |
| 247 | |
| 248 | else |
| 249 | last = x |
| 250 | end |
| 251 | end |
| 252 | decode_error(str, i, "expected closing quote for string") |
| 253 | end |
| 254 | |
| 255 | |
| 256 | local function parse_number(str, i) |
| 257 | local x = next_char(str, i, delim_chars) |
| 258 | local s = str:sub(i, x - 1) |
| 259 | local n = tonumber(s) |
| 260 | if not n then |
| 261 | decode_error(str, i, "invalid number '" .. s .. "'") |
| 262 | end |
| 263 | return n, x |
| 264 | end |
| 265 | |
| 266 | |
| 267 | local function parse_literal(str, i) |
| 268 | local x = next_char(str, i, delim_chars) |
| 269 | local word = str:sub(i, x - 1) |
| 270 | if not literals[word] then |
| 271 | decode_error(str, i, "invalid literal '" .. word .. "'") |
| 272 | end |
| 273 | return literal_map[word], x |
| 274 | end |
| 275 | |
| 276 | |
| 277 | local function parse_array(str, i) |
| 278 | local res = {} |
| 279 | local n = 1 |
| 280 | i = i + 1 |
| 281 | while 1 do |
| 282 | local x |
| 283 | i = next_char(str, i, space_chars, true) |
| 284 | -- Empty / end of array? |
| 285 | if str:sub(i, i) == "]" then |
| 286 | i = i + 1 |
| 287 | break |
| 288 | end |
| 289 | -- Read token |
| 290 | x, i = parse(str, i) |
| 291 | res[n] = x |
| 292 | n = n + 1 |
| 293 | -- Next token |
| 294 | i = next_char(str, i, space_chars, true) |
| 295 | local chr = str:sub(i, i) |
| 296 | i = i + 1 |
| 297 | if chr == "]" then break end |
| 298 | if chr ~= "," then decode_error(str, i, "expected ']' or ','") end |
| 299 | end |
| 300 | return res, i |
| 301 | end |
| 302 | |
| 303 | |
| 304 | local function parse_object(str, i) |
| 305 | local res = {} |
| 306 | i = i + 1 |
| 307 | while 1 do |
| 308 | local key, val |
| 309 | i = next_char(str, i, space_chars, true) |
| 310 | -- Empty / end of object? |
| 311 | if str:sub(i, i) == "}" then |
| 312 | i = i + 1 |
| 313 | break |
| 314 | end |
| 315 | -- Read key |
| 316 | if str:sub(i, i) ~= '"' then |
| 317 | decode_error(str, i, "expected string for key") |
| 318 | end |
| 319 | key, i = parse(str, i) |
| 320 | -- Read ':' delimiter |
| 321 | i = next_char(str, i, space_chars, true) |
| 322 | if str:sub(i, i) ~= ":" then |
| 323 | decode_error(str, i, "expected ':' after key") |
| 324 | end |
| 325 | i = next_char(str, i + 1, space_chars, true) |
| 326 | -- Read value |
| 327 | val, i = parse(str, i) |
| 328 | -- Set |
| 329 | res[key] = val |
| 330 | -- Next token |
| 331 | i = next_char(str, i, space_chars, true) |
| 332 | local chr = str:sub(i, i) |
| 333 | i = i + 1 |
| 334 | if chr == "}" then break end |
| 335 | if chr ~= "," then decode_error(str, i, "expected '}' or ','") end |
| 336 | end |
| 337 | return res, i |
| 338 | end |
| 339 | |
| 340 | |
| 341 | local char_func_map = { |
| 342 | [ '"' ] = parse_string, |
| 343 | [ "0" ] = parse_number, |
| 344 | [ "1" ] = parse_number, |
| 345 | [ "2" ] = parse_number, |
| 346 | [ "3" ] = parse_number, |
| 347 | [ "4" ] = parse_number, |
| 348 | [ "5" ] = parse_number, |
| 349 | [ "6" ] = parse_number, |
| 350 | [ "7" ] = parse_number, |
| 351 | [ "8" ] = parse_number, |
| 352 | [ "9" ] = parse_number, |
| 353 | [ "-" ] = parse_number, |
| 354 | [ "t" ] = parse_literal, |
| 355 | [ "f" ] = parse_literal, |
| 356 | [ "n" ] = parse_literal, |
| 357 | [ "[" ] = parse_array, |
| 358 | [ "{" ] = parse_object, |
| 359 | } |
| 360 | |
| 361 | |
| 362 | parse = function(str, idx) |
| 363 | local chr = str:sub(idx, idx) |
| 364 | local f = char_func_map[chr] |
| 365 | if f then |
| 366 | return f(str, idx) |
| 367 | end |
| 368 | decode_error(str, idx, "unexpected character '" .. chr .. "'") |
| 369 | end |
| 370 | |
| 371 | |
| 372 | function json.decode(str) |
| 373 | if type(str) ~= "string" then |
| 374 | error("expected argument of type string, got " .. type(str)) |
| 375 | end |
| 376 | return ( parse(str, next_char(str, 1, space_chars, true)) ) |
| 377 | end |
| 378 | |
| 379 | |
| 380 | return json |