summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-25 19:15:32 +0800
committerOpportunityLiu <[email protected]>2019-07-25 19:15:32 +0800
commit68f96b225c4da758a87a90f5723025be7b2fafc1 (patch)
treee4452c4851e3b0cd99125c67df218f09a65850f8
parent5e8049c85f976305206ed2117b71168b1972704c (diff)
add binary mode
-rw-r--r--tests/modules/string/serialize/test.lua7
-rw-r--r--xmake/core/base/serialize.lua43
-rw-r--r--xmake/core/base/string.lua8
3 files changed, 48 insertions, 10 deletions
diff --git a/tests/modules/string/serialize/test.lua b/tests/modules/string/serialize/test.lua
index a85868e7e..1ab3852ba 100644
--- a/tests/modules/string/serialize/test.lua
+++ b/tests/modules/string/serialize/test.lua
@@ -1,6 +1,9 @@
-function roundtrip(v)
- return string.serialize(v):deserialize()
+function roundtrip(round0)
+ local round1 = string.serialize(round0, false):deserialize()
+ local round2 = string.serialize(round1, true):deserialize()
+ local round3 = string.serialize(round2, {binary=true}):deserialize()
+ return round3
end
function test_number(t)
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
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index 30c374def..6579ef7de 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -203,13 +203,13 @@ end
-- serialize to string from the given object
--
--- @param deflate deflate empty characters
+-- @param opt serialize options
+-- e.g. { strip = true, binary = false, indent = true }
--
-- @return string, errors
--
-function string.serialize(object, deflate)
- deflate = not not deflate
- return serialize.save(object, { strip = deflate, indent = not deflate })
+function string.serialize(object, opt)
+ return serialize.save(object, opt)
end
-- deserialize string to object