lljson.slencode() could not use the lljson metamethods and metatables
needsĀ info
SuzannaLinn Resident
lljson.slencode()
uses the same metamethods and metatables than lljson.encode()
:- __tojson
- __len
- array_mt
- empty_array_mt
Since the purpose of
lljson.slencode()/sldecode()
is to decode the same table that was encoded, the options to change the encoding are not useful.Not using them would allow to:
- export the table to an external resource, using the metamethods and metatables to configure it.
- store the same table internally as it is, without using the metamethods and metatables.
And it would avoid conflicts with:
- a __len metamethod used for something else (like getting the length of a dictionary table).
Log In
H
Harold Linden
marked this post as
needsĀ info
SuzannaLinn Resident
Harold Linden
My interpretation is that:
- encode()is to export formatted JSON to an external resource.
- slencode()is to get JSON that can be decoded back to an identical table withsldecode().
With this point of view is not useful to have metamethods with
slencode()
since it generates JSON that can't be decoded back.If
slencode()
didn't use metamethods, we could, from the same table:- get formatted JSON with encode()for external uses.
- get decodable JSON with slencode()for internal uses.
Anyway, we can do this:
local mt = getmetatable(myTab)
setmetatable(myTab, nil)
json = lljson.slencode(myTab)
setmetatable(myTab, mt)
I think that ignoring
__len
in slencode()
would be good for scripters who are not interested in a deep knowledge of the lljson
library but just to use it to store tables in linkset data, in case that they use __len
for something else and get unexpected results.H
Harold Linden
We definitely want to use
__tojson
, because that's how we present a view of the object that's usable for serialization. You may want to replace entire objects with reference strings / indices instead so that you can represent data that would otherwise have un-encodable reference cycles.slencode()
preserves _type semantics_, but there are a number of cases where the default serialization strategy isn't going to work for people, and __tojson
gives them an escape hatch.I think ignoring
__len
makes sense though. Any thoughts?