Software /
code /
prosody
Changeset
9485:c667887d78ad
util.serialization: Simpler metatable pre-processing
It was too difficult to describe what it did.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 12 Oct 2018 00:13:24 +0200 |
parents | 9484:1d1541630c20 |
children | 9486:20aad0108999 |
files | spec/util_serialization_spec.lua util/serialization.lua |
diffstat | 2 files changed, 20 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/spec/util_serialization_spec.lua Thu Oct 11 23:00:45 2018 +0200 +++ b/spec/util_serialization_spec.lua Fri Oct 12 00:13:24 2018 +0200 @@ -44,6 +44,14 @@ test({foo={[100]={{"bar"},{baz=1}}}}); test({["goto"] = {["function"]={["do"]="keywords"}}}); end); + + it("can serialize with metatables", function () + local s = serialization.new({ freeze = true }); + local t = setmetatable({ a = "hi" }, { __freeze = function (t) return { t.a } end }); + local rt = serialization.deserialize(s(t)); + assert.same({"hi"}, rt); + end); + end); end);
--- a/util/serialization.lua Thu Oct 11 23:00:45 2018 +0200 +++ b/util/serialization.lua Fri Oct 12 00:13:24 2018 +0200 @@ -139,25 +139,23 @@ end o[t] = true; - if freeze then + + if freeze == true then -- opportunity to do pre-serialization local mt = getmetatable(t); - local fr = (type(freeze) == "table" and freeze[mt]); - local mf = mt and mt.__freeze; - local tag; - if type(fr) == "string" then - tag = fr; - fr = mf; - elseif mt then - tag = mt.__type; - end - if type(fr) == "function" then - t = fr(t); - if type(tag) == "string" then - o[l], l = tag, l + 1; + if type(mt) == "table" then + local tag = mt.__name; + local fr = mt.__freeze; + + if type(fr) == "function" then + t = fr(t); + if type(tag) == "string" then + o[l], l = tag, l + 1; + end end end end + o[l], l = tstart, l + 1; local indent = s_rep(indentwith, d); local numkey = 1;