summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-07-21 00:32:46 +0800
committerruki <[email protected]>2020-07-21 00:32:46 +0800
commita985a18ea4fd2bd8f5bb56f55292a19ba584409c (patch)
treefb777941ecf1fc73dbd290ce03610cb91278f13b
parent8aa0279fa83f1d90f4c853da316de4e54c3d33d7 (diff)
improve json
-rw-r--r--core/src/xmake/engine.c4
-rw-r--r--xmake/core/base/io.lua11
-rw-r--r--xmake/core/base/json.lua130
-rw-r--r--xmake/core/sandbox/modules/import/core/base/json.lua46
4 files changed, 184 insertions, 7 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index fb6205ac5..dd26486ad 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -854,9 +854,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c
lua_setglobal(engine->lua, "curses");
#endif
- // bind json
+ // bind cjson
luaopen_cjson(engine->lua);
- lua_setglobal(engine->lua, "json");
+ lua_setglobal(engine->lua, "cjson");
// init host
xm_engine_init_host(engine);
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index 796704dd9..936bd5868 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -158,6 +158,11 @@ function _file:size()
end
-- read data from file
+--
+-- @param fmt the reading format
+-- @param opt the options
+-- - continuation (concat string with the given continuation characters)
+--
function _file:read(fmt, opt)
-- ensure opened
@@ -549,6 +554,12 @@ function io.stdfile(filepath)
end
-- open file
+--
+-- @param filepath the file path
+-- @param mode the open mode, e.g. 'r', 'rb', 'w+', 'a+', ..
+-- @param opt the options
+-- - encoding, e.g. utf8, utf16, utf16le, utf16be ..
+--
function io.open(filepath, mode, opt)
-- check
diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua
index 26dbce173..ef17bf822 100644
--- a/xmake/core/base/json.lua
+++ b/xmake/core/base/json.lua
@@ -19,11 +19,133 @@
--
-- define module: json
-local json = json or {}
+local json = json or {}
+local cjson = cjson or {}
--- hide some cjson members
-json._VERSION = nil
-json._NAME = nil
+-- laod modules
+local io = require("base/io")
+local utils = require("base/utils")
+
+-- export null
+json.null = cjson.null
+
+-- decode json string to the lua table
+--
+-- @param jsonstr the json string
+-- @param opt the options
+-- - max_depth
+-- - invalid_numbers
+--
+-- @return the lua table
+--
+function json.decode(jsonstr, opt)
+ local cjsonobj = cjson
+ if opt then
+ if opt.max_depth then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.decode_max_depth(opt.max_depth)
+ end
+ if opt.invalid_numbers then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.decode_invalid_numbers(opt.invalid_numbers)
+ end
+ end
+ local ok, luatable_or_errors = utils.trycall(cjsonobj.decode, nil, jsonstr)
+ if not ok then
+ return nil, string.format("decode json failed, %s", luatable_or_errors)
+ end
+ return luatable_or_errors
+end
+
+-- encode lua table to the json string
+--
+-- @param luatable the lua table
+-- @param opt the options
+-- - max_depth
+-- - invalid_numbers
+-- - keep_buffer
+-- - number_precision
+--
+-- @return the json string
+--
+function json.encode(luatable, opt)
+ local cjsonobj = cjson
+ if opt then
+ if opt.max_depth then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.encode_max_depth(opt.max_depth)
+ end
+ if opt.invalid_numbers then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.encode_invalid_numbers(opt.invalid_numbers)
+ end
+ if opt.keep_buffer then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.encode_keep_buffer(opt.keep_buffer)
+ end
+ if opt.number_precision then
+ if cjsonobj == cjson then
+ cjsonobj = cjson.new()
+ end
+ cjsonobj.encode_number_precision(opt.number_precision)
+ end
+ end
+ local ok, jsonstr_or_errors = utils.trycall(cjsonobj.encode, nil, luatable)
+ if not ok then
+ return nil, string.format("encode json failed, %s", jsonstr_or_errors)
+ end
+ return jsonstr_or_errors
+end
+
+-- load json file to the lua table
+--
+-- @param filepath the json file path
+-- @param opt the options
+-- - encoding for io/file, e.g. utf8, utf16, utf16le, utf16be ..
+-- - continuation for io/read (concat string with the given continuation characters)
+-- - max_depth
+-- - invalid_numbers
+--
+-- @return the lua table
+--
+function json.loadfile(filepath, opt)
+ local filedata, errors = io.readfile(filepath, opt)
+ if not filedata then
+ return nil, errors
+ end
+ return json.decode(filedata, opt)
+end
+
+-- save lua table to the json file
+--
+-- @param filepath the json file path
+-- @param luatable the lua table
+-- @param opt the options
+-- - encoding for io/file, e.g. utf8, utf16, utf16le, utf16be ..
+-- - max_depth
+-- - invalid_numbers
+-- - keep_buffer
+-- - number_precision
+--
+-- @return the json string
+--
+function json.savefile(filepath, luatable, opt)
+ local jsonstr, errors = json.encode(luatable, opt)
+ if not jsonstr then
+ return false, errors
+ end
+ return io.writefile(filepath, jsonstr, opt)
+end
-- return module: json
return json
diff --git a/xmake/core/sandbox/modules/import/core/base/json.lua b/xmake/core/sandbox/modules/import/core/base/json.lua
index c8f428298..25891a8a3 100644
--- a/xmake/core/sandbox/modules/import/core/base/json.lua
+++ b/xmake/core/sandbox/modules/import/core/base/json.lua
@@ -18,7 +18,51 @@
-- @file json.lua
--
+-- define module
+local sandbox_core_base_json = sandbox_core_base_json or {}
+
-- load modules
-return require("base/json")
+local json = require("base/json")
+local raise = require("sandbox/modules/raise")
+
+-- inherit some builtin interfaces
+sandbox_core_base_json.null = json.null
+
+-- decode the json string to the lua table
+function sandbox_core_base_json.decode(jsonstr, opt)
+ local luatable, errors = json.decode(jsonstr, opt)
+ if not luatable then
+ raise(errors)
+ end
+ return luatable
+end
+
+-- encode the lua table to the json string
+function sandbox_core_base_json.encode(luatable, opt)
+ local jsonstr, errors = json.encode(luatable, opt)
+ if not jsonstr then
+ raise(errors)
+ end
+ return jsonstr
+end
+
+-- load json file to the lua table
+function sandbox_core_base_json.loadfile(filepath, opt)
+ local luatable, errors = json.loadfile(filepath, opt)
+ if not luatable then
+ raise(errors)
+ end
+ return luatable
+end
+-- save lua table to the json file
+function sandbox_core_base_json.savefile(filepath, luatable, opt)
+ local ok, errors = json.savefile(filepath, luatable, opt)
+ if not ok then
+ raise(errors)
+ end
+ return ok
+end
+-- return module
+return sandbox_core_base_json