diff options
| author | OpportunityLiu <[email protected]> | 2019-07-25 19:15:32 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-25 19:15:32 +0800 |
| commit | 68f96b225c4da758a87a90f5723025be7b2fafc1 (patch) | |
| tree | e4452c4851e3b0cd99125c67df218f09a65850f8 /xmake/core/base/serialize.lua | |
| parent | 5e8049c85f976305206ed2117b71168b1972704c (diff) | |
add binary mode
Diffstat (limited to 'xmake/core/base/serialize.lua')
| -rw-r--r-- | xmake/core/base/serialize.lua | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/xmake/core/base/serialize.lua b/xmake/core/base/serialize.lua index 84c74cd34..8b5b233b1 100644 --- a/xmake/core/base/serialize.lua +++ b/xmake/core/base/serialize.lua @@ -138,6 +138,10 @@ function serialize._maketable(object, opt, level) table.insert(s, indent .. k .. con .. v) end end + + if #s == 0 then + return opt.indent and "{ }" or "{}" + end return headstr .. table.concat(s, opt.indent and ",\n" or ",") .. tailstr end @@ -189,7 +193,11 @@ end function serialize.save(object, opt) -- init options - opt = opt or {} + if opt == true then + opt = { strip = true, binary = false, indent = false } + elseif opt == false or opt == nil then + opt = { strip = false, binary = false, indent = true } + end -- make string local result, errors = serialize._make(object, opt, 0) @@ -198,7 +206,19 @@ function serialize.save(object, opt) if errors ~= nil then return nil, errors end - return result + + if not opt.binary then + return result + end + + -- binary mode + local dump, lerr = serialize._dump(loadstring("return " .. result), true) + if lerr ~= nil then + return nil, lerr + end + + -- return shorter representation + return (#dump < #result) and dump or result end -- load table from string in table @@ -206,7 +226,14 @@ function serialize._load(str) -- load table as script local result = nil - local script, errors = loadstring("return " .. str, str) + + local binary = str:startswith("\27LJ") + + if not binary then + str = "return " .. str + end + + local script, errors = loadstring(str) if script then -- load object @@ -217,8 +244,16 @@ function serialize._load(str) -- error errors = object else + local data + if binary then + data = "<binary data>" + elseif #str > 20 then + data = str:sub(8, 17) .. "..." + else + data = str:sub(8) + end -- error - errors = string.format("cannot deserialize string: %s", str) + errors = string.format("cannot deserialize string: %s", data) end end |
