diff options
| author | OpportunityLiu <[email protected]> | 2019-07-25 19:45:45 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-25 19:46:54 +0800 |
| commit | b724d37e01b4a12d1b5eea3a9a866ba4b8c4e46d (patch) | |
| tree | a8d30f410a7e99824923633c956588bcc18498b2 | |
| parent | 68f96b225c4da758a87a90f5723025be7b2fafc1 (diff) | |
fix style
| -rw-r--r-- | tests/modules/string/serialize/test.lua | 4 | ||||
| -rw-r--r-- | xmake/core/base/serialize.lua | 30 |
2 files changed, 25 insertions, 9 deletions
diff --git a/tests/modules/string/serialize/test.lua b/tests/modules/string/serialize/test.lua index 1ab3852ba..567bee87f 100644 --- a/tests/modules/string/serialize/test.lua +++ b/tests/modules/string/serialize/test.lua @@ -3,7 +3,9 @@ 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 + local round4 = string.serialize(round3, {indent=16}):deserialize() + local round5 = string.serialize(round4, {indent=" \r\n\t"}):deserialize() + return round5 end function test_number(t) diff --git a/xmake/core/base/serialize.lua b/xmake/core/base/serialize.lua index 8b5b233b1..4a9392ebe 100644 --- a/xmake/core/base/serialize.lua +++ b/xmake/core/base/serialize.lua @@ -86,16 +86,12 @@ function serialize._maketable(object, opt, level) -- make indent local indent = "" if opt.indent then - indent = string.rep(" ", level) + indent = string.rep(opt.indent, level) end -- make head - local headstr - if opt.indent then - headstr = (level > 0 and "\n" or "") .. indent .. "{\n" - else - headstr = "{" - end + local headstr = opt.indent and "{\n" or "{" + -- make tail local tailstr if opt.indent then @@ -107,7 +103,7 @@ function serialize._maketable(object, opt, level) -- make body local s = {} if opt.indent then - indent = indent .. " " + indent = string.rep(opt.indent, level + 1) end if isarr then @@ -199,6 +195,24 @@ function serialize.save(object, opt) opt = { strip = false, binary = false, indent = true } end + if not opt.indent then + opt.indent = false + elseif type(opt.indent) == "boolean" then + opt.indent = " " + elseif type(opt.indent) == "number" then + if opt.indent < 0 then + opt.indent = false + else + opt.indent = string.rep(" ", opt.indent) + end + elseif type(opt.indent) == "string" then + if not opt.indent:match("^%s+$") then + return nil, "invalid opt.indent, only whitespaces are accepted" + end + else + return nil, "invalid opt.indent, should be boolean, number or string" + end + -- make string local result, errors = serialize._make(object, opt, 0) |
