diff options
Diffstat (limited to 'xmake/core/base/json.lua')
| -rw-r--r-- | xmake/core/base/json.lua | 54 |
1 files changed, 34 insertions, 20 deletions
diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 4c619e69a..3c84642c6 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -27,24 +27,25 @@ local os = require("base/os") local utils = require("base/utils") -- export null +json.purenull = {} +setmetatable(json.purenull, { + __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}) 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}) + json.null = json.purenull end function json._pure_kind_of(obj) @@ -54,7 +55,7 @@ function json._pure_kind_of(obj) if json.is_marked_as_array(obj) then return "array" end - if obj == json.null then + if obj == json.purenull then return "nil" end local i = 1 @@ -116,7 +117,12 @@ function json._pure_parse_str_val(str, pos, val) end function json._pure_parse_num_val(str, pos) - local num_str = str:match('^-?%d+%.?%d*[eE]?[+-]?%d*', pos) + local num_str + if str:sub(pos, pos + 1) == "0x" then + num_str = str:match('^-?0[xX][0-9a-fA-F]+', pos) + else + num_str = str:match('^-?%d+%.?%d*[eE]?[+-]?%d*', pos) + end local val = tonumber(num_str) if not val then os.raise("error parsing number at position %d", pos) @@ -212,7 +218,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.purenull} 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 @@ -256,7 +262,11 @@ end -- @return the lua table -- function json.decode(jsonstr, opt) - local ok, luatable_or_errors = utils.trycall(cjson and cjson.decode or json._pure_decode, nil, jsonstr) + local decode = cjson and cjson.decode or json._pure_decode + if opt and opt.pure then + decode = json._pure_decode + end + local ok, luatable_or_errors = utils.trycall(decode, nil, jsonstr) if not ok then return nil, string.format("decode json failed, %s", luatable_or_errors) end @@ -271,7 +281,11 @@ end -- @return the json string -- function json.encode(luatable, opt) - local ok, jsonstr_or_errors = utils.trycall(cjson and cjson.encode or json._pure_encode, nil, luatable) + local encode = cjson and cjson.encode or json._pure_encode + if opt and opt.pure then + encode = json._pure_encode + end + local ok, jsonstr_or_errors = utils.trycall(encode, nil, luatable) if not ok then return nil, string.format("encode json failed, %s", jsonstr_or_errors) end |
