diff options
| author | ruki <[email protected]> | 2023-01-19 23:26:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-01-19 23:26:35 +0800 |
| commit | e8228184e4325be6adb4d44e2a21189124807fc8 (patch) | |
| tree | b91db5e07b642d491ded81579894cb06e930a83c /xmake/core/base/json.lua | |
| parent | abf56ce85047cee83c245f56b40608368007e4a5 (diff) | |
improve json.null
Diffstat (limited to 'xmake/core/base/json.lua')
| -rw-r--r-- | xmake/core/base/json.lua | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 45b629393..dcd3ffaa0 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -28,7 +28,25 @@ local os = require("base/os") local utils = require("base/utils") -- export null -json.null = cjson and cjson.null or {} +if cjson then + json.null = cjson.null +else + json.null = {} + setmetatable(json.null, { + __is_json_null = true, + __eq = function (obj) + if type(obj) == "table" then + local mt = getmetatable(obj) + if mt and mt.__is_json_null then + return true + end + end + return false + end, + __tostring = function() + return "null" + end}) +end function json._pure_kind_of(obj) if type(obj) ~= "table" then @@ -37,6 +55,9 @@ function json._pure_kind_of(obj) if json.is_marked_as_array(obj) then return "array" end + if obj == json.null then + return "nil" + end local i = 1 for _ in pairs(obj) do if obj[i] ~= nil then @@ -119,7 +140,7 @@ function json._pure_stringify(obj, as_key) end s[#s + 1] = '[' for i, val in ipairs(obj) do - if i > 1 then s[#s + 1] = ', ' end + if i > 1 then s[#s + 1] = ',' end s[#s + 1] = json._pure_stringify(val) end s[#s + 1] = ']' @@ -129,7 +150,7 @@ function json._pure_stringify(obj, as_key) end s[#s + 1] = '{' for k, v in pairs(obj) do - if #s > 1 then s[#s + 1] = ', ' end + if #s > 1 then s[#s + 1] = ',' end s[#s + 1] = json._pure_stringify(k, true) s[#s + 1] = ':' s[#s + 1] = json._pure_stringify(v) @@ -145,7 +166,7 @@ function json._pure_stringify(obj, as_key) elseif kind == "boolean" then return tostring(obj) elseif kind == "nil" then - return 'null' + return "null" else os.raise('unjsonifiable type: ' .. kind .. '.') end @@ -198,7 +219,7 @@ function json._pure_parse(str, pos, end_delim) -- end of an object or array. return nil, pos + 1 else - local literals = {['true'] = true, ['false'] = false, ['null'] = json.null} + local literals = {["true"] = true, ["false"] = false, ["null"] = json.null} for lit_str, lit_val in pairs(literals) do local lit_end = pos + #lit_str - 1 if str:sub(pos, lit_end) == lit_str then |
