From 35ec8577f2d81e472a8bc0db9ce66215f3182b4f Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Jul 2024 23:39:27 +0800 Subject: add pure json test --- xmake/core/base/json.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'xmake/core/base/json.lua') diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 4c619e69a..bf1dfae3e 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -256,7 +256,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 +275,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 -- cgit v1.3.1 From 6289723f9bd7dbc44bdcfa32ad4fe9bece0e3840 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Jul 2024 23:41:06 +0800 Subject: add hex support for pure json --- tests/modules/json/test.lua | 2 +- xmake/core/base/json.lua | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'xmake/core/base/json.lua') diff --git a/tests/modules/json/test.lua b/tests/modules/json/test.lua index 9657a2e5e..ad0e84ac6 100755 --- a/tests/modules/json/test.lua +++ b/tests/modules/json/test.lua @@ -63,7 +63,7 @@ function test_pure_json_decode(t) t:are_equal(json_pure_decode('{"a":[], "b":[1,2], "c":{"a":1}}'), {a = {}, b = {1,2}, c = {a = 1}}) t:are_equal(json_pure_decode('[1,"2"]'), {1, "2"}) t:are_equal(json_pure_decode('[1,"2", {"a":1, "b":true}]'), {1, "2", {a = 1, b = true}}) --- t:are_equal(json_pure_decode('[1,0xa,0xdeadbeef, 0xffffffff,-1]'), {1, 0xa, 0xdeadbeef, 0xffffffff, -1}) + t:are_equal(json_pure_decode('[1,0xa,0xdeadbeef, 0xffffffff,-1]'), {1, 0xa, 0xdeadbeef, 0xffffffff, -1}) end function test_pure_json_encode(t) diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index bf1dfae3e..487e4b062 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -26,6 +26,8 @@ local io = require("base/io") local os = require("base/os") local utils = require("base/utils") +cjson = nil + -- export null if cjson then json.null = cjson.null @@ -116,7 +118,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) -- cgit v1.3.1 From a8bf62a7022a9bdcbed2e0c3a03abe37153962de Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Jul 2024 23:41:38 +0800 Subject: fix null for test --- tests/modules/json/test.lua | 20 +++----------------- xmake/core/base/json.lua | 1 + xmake/core/sandbox/modules/import/core/base/json.lua | 1 + 3 files changed, 5 insertions(+), 17 deletions(-) (limited to 'xmake/core/base/json.lua') diff --git a/tests/modules/json/test.lua b/tests/modules/json/test.lua index ad0e84ac6..a09f0918a 100755 --- a/tests/modules/json/test.lua +++ b/tests/modules/json/test.lua @@ -1,21 +1,7 @@ import("core.base.json") local json_null = json.null -local json_pure_null = {} -debug.setmetatable(json_pure_null, { - __is_json_null = true, - __eq = function (obj) - if type(obj) == "table" then - local mt = debug.getmetatable(obj) - if mt and mt.__is_json_null then - return true - end - end - return false - end, - __tostring = function() - return "null" - end}) +local json_pure_null = json.purenull function json_decode(jsonstr) return json.decode(jsonstr) @@ -59,7 +45,7 @@ function test_pure_json_decode(t) t:are_equal(json_pure_decode('[]'), {}) t:are_equal(json.is_marked_as_array(json_pure_decode('[]')), true) t:are_equal(not json.is_marked_as_array(json_pure_decode('{}')), true) --- t:are_equal(json_pure_decode('{"a":1, "b":"2", "c":true, "d":false, "e":null, "f":[]}'), {a = 1, b = "2", c = true, d = false, e = json_pure_null, f = {}}) + t:are_equal(json_pure_decode('{"a":1, "b":"2", "c":true, "d":false, "e":null, "f":[]}'), {a = 1, b = "2", c = true, d = false, e = json_pure_null, f = {}}) t:are_equal(json_pure_decode('{"a":[], "b":[1,2], "c":{"a":1}}'), {a = {}, b = {1,2}, c = {a = 1}}) t:are_equal(json_pure_decode('[1,"2"]'), {1, "2"}) t:are_equal(json_pure_decode('[1,"2", {"a":1, "b":true}]'), {1, "2", {a = 1, b = true}}) @@ -69,7 +55,7 @@ end function test_pure_json_encode(t) t:are_equal(json_pure_encode({}), '{}') t:are_equal(json_pure_encode(json.mark_as_array({})), '[]') --- t:are_equal(json_pure_encode({json_pure_null, 1, "2", false, true}), '[null,1,"2",false,true]') + t:are_equal(json_pure_encode({json_pure_null, 1, "2", false, true}), '[null,1,"2",false,true]') t:are_equal(json_pure_encode({1, "2", {a = 1}}), '[1,"2",{"a":1}]') t:are_equal(json_pure_encode({1, "2", {b = true}}), '[1,"2",{"b":true}]') t:are_equal(json_pure_encode(json.mark_as_array({1, 0xa, 0xdeadbeef, 0xffffffff, -1})), '[1,10,3735928559,4294967295,-1]') diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 487e4b062..b68751627 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -47,6 +47,7 @@ else __tostring = function() return "null" end}) + json.purenull = json.null end function json._pure_kind_of(obj) diff --git a/xmake/core/sandbox/modules/import/core/base/json.lua b/xmake/core/sandbox/modules/import/core/base/json.lua index ed551993d..68f70a331 100644 --- a/xmake/core/sandbox/modules/import/core/base/json.lua +++ b/xmake/core/sandbox/modules/import/core/base/json.lua @@ -27,6 +27,7 @@ local raise = require("sandbox/modules/raise") -- inherit some builtin interfaces sandbox_core_base_json.null = json.null +sandbox_core_base_json.purenull = json.purenull sandbox_core_base_json.mark_as_array = json.mark_as_array sandbox_core_base_json.is_marked_as_array = json.is_marked_as_array -- cgit v1.3.1 From c11d1ace385dee895b7c4a5434d62f909a129ee4 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Jul 2024 23:43:40 +0800 Subject: revert test code --- xmake/core/base/json.lua | 2 -- 1 file changed, 2 deletions(-) (limited to 'xmake/core/base/json.lua') diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index b68751627..44fab0a9a 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -26,8 +26,6 @@ local io = require("base/io") local os = require("base/os") local utils = require("base/utils") -cjson = nil - -- export null if cjson then json.null = cjson.null -- cgit v1.3.1 From f158b9996d8a56ab9777f3b794224eab165660e1 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 2 Jul 2024 23:44:26 +0800 Subject: fix json.purenull --- xmake/core/base/json.lua | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'xmake/core/base/json.lua') diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 44fab0a9a..3c84642c6 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -27,25 +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.purenull = json.null + json.null = json.purenull end function json._pure_kind_of(obj) @@ -55,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 @@ -218,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 -- cgit v1.3.1