2017-04-14 16:50:33 +00:00
|
|
|
--[[--
|
|
|
|
A simple serialization function which won't do uservalues, functions, or loops.
|
2014-11-14 15:33:52 +00:00
|
|
|
]]
|
|
|
|
|
2015-10-03 06:18:47 +00:00
|
|
|
local isUbuntuTouch = os.getenv("UBUNTU_APPLICATION_ISOLATION") ~= nil
|
2014-11-14 15:33:52 +00:00
|
|
|
local insert = table.insert
|
2016-05-02 02:39:31 +00:00
|
|
|
local indent_prefix = " "
|
2014-11-14 15:33:52 +00:00
|
|
|
|
2022-06-11 17:06:06 +00:00
|
|
|
local function _serialize(what, outt, indent, max_lv, history, pairs_func)
|
2014-11-14 15:33:52 +00:00
|
|
|
if not max_lv then
|
|
|
|
max_lv = math.huge
|
|
|
|
end
|
|
|
|
|
|
|
|
if indent > max_lv then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(what) == "table" then
|
2014-11-16 16:27:37 +00:00
|
|
|
history = history or {}
|
|
|
|
for up, item in ipairs(history) do
|
|
|
|
if item == what then
|
|
|
|
insert(outt, "nil --[[ LOOP:\n")
|
2016-05-02 02:39:31 +00:00
|
|
|
insert(outt, string.rep(indent_prefix, indent - up))
|
2014-11-16 16:27:37 +00:00
|
|
|
insert(outt, "^------- ]]")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2014-11-14 15:33:52 +00:00
|
|
|
local new_history = { what, unpack(history) }
|
|
|
|
local didrun = false
|
|
|
|
insert(outt, "{")
|
2022-06-11 17:06:06 +00:00
|
|
|
for k, v in pairs_func(what) do
|
2014-11-14 15:33:52 +00:00
|
|
|
insert(outt, "\n")
|
2016-05-02 02:39:31 +00:00
|
|
|
insert(outt, string.rep(indent_prefix, indent+1))
|
2014-11-14 15:33:52 +00:00
|
|
|
insert(outt, "[")
|
2022-06-11 17:06:06 +00:00
|
|
|
_serialize(k, outt, indent+1, max_lv, new_history, pairs_func)
|
2014-11-14 15:33:52 +00:00
|
|
|
insert(outt, "] = ")
|
2022-06-11 17:06:06 +00:00
|
|
|
_serialize(v, outt, indent+1, max_lv, new_history, pairs_func)
|
2020-11-12 19:38:11 +00:00
|
|
|
insert(outt, ",")
|
2014-11-14 15:33:52 +00:00
|
|
|
didrun = true
|
|
|
|
end
|
|
|
|
if didrun then
|
|
|
|
insert(outt, "\n")
|
2016-05-02 02:39:31 +00:00
|
|
|
insert(outt, string.rep(indent_prefix, indent))
|
2014-11-14 15:33:52 +00:00
|
|
|
end
|
|
|
|
insert(outt, "}")
|
|
|
|
elseif type(what) == "string" then
|
|
|
|
insert(outt, string.format("%q", what))
|
2015-10-03 06:18:47 +00:00
|
|
|
elseif type(what) == "number" then
|
|
|
|
if isUbuntuTouch then
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @fixme The `SDL_CreateRenderer` function in Ubuntu touch somehow
|
2016-03-08 06:52:52 +00:00
|
|
|
-- use a strange locale that formats number like this: 1.10000000000000g+02
|
2015-10-03 06:18:47 +00:00
|
|
|
-- which cannot be recognized by loadfile after the number is dumped.
|
|
|
|
-- Here the workaround is to preserve enough precision in "%.13e" format.
|
|
|
|
insert(outt, string.format("%.13e", what))
|
|
|
|
else
|
|
|
|
insert(outt, tostring(what))
|
|
|
|
end
|
|
|
|
elseif type(what) == "boolean" then
|
2014-11-14 15:33:52 +00:00
|
|
|
insert(outt, tostring(what))
|
|
|
|
elseif type(what) == "function" then
|
2016-03-08 06:52:52 +00:00
|
|
|
insert(outt, tostring(what))
|
2014-11-14 15:33:52 +00:00
|
|
|
elseif type(what) == "nil" then
|
|
|
|
insert(outt, "nil")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-14 16:50:33 +00:00
|
|
|
--[[--Serializes whatever is in `data` to a string that is parseable by Lua.
|
2014-11-14 15:33:52 +00:00
|
|
|
|
2017-04-14 16:50:33 +00:00
|
|
|
You can optionally specify a maximum recursion depth in `max_lv`.
|
|
|
|
@function dump
|
|
|
|
@param data the object you want serialized (table, string, number, boolean, nil)
|
|
|
|
@param max_lv optional maximum recursion depth
|
2014-11-14 15:33:52 +00:00
|
|
|
--]]
|
2020-11-12 19:38:11 +00:00
|
|
|
local function dump(data, max_lv, ordered)
|
2014-11-14 15:33:52 +00:00
|
|
|
local out = {}
|
2022-06-11 17:06:06 +00:00
|
|
|
local pairs_func = ordered and require("ffi/util").orderedPairs or pairs
|
|
|
|
_serialize(data, out, 0, max_lv, nil, pairs_func)
|
2014-11-14 15:33:52 +00:00
|
|
|
return table.concat(out)
|
|
|
|
end
|
|
|
|
|
|
|
|
return dump
|