Meta method __tojson for lljson.encode
inĀ progress
WolfGang Senizen
It would be useful to have to allow for encoding of more complex data structures, also to allow tables that have functions to be encoded by specifying them to not be included
Probably best for
__tojson
to return what we want to be encoded rather than actual json?local Nested = {}
Nested.__index = Nested
function Nested:__tojson()
return {
name= self.name,
child= if self.child then self.child else nil
}
end
function Nested.new(name,parent)
return setmetatable({
name=name,
parent=parent,
child=nil
},Nested)
end
local root = Nested.new("root")
root.child = Nested.new("child1",root)
root.child.child = Nested.new("child2",root.child)
ll.OwnerSay(lljson.encode(root))
Possibly this is deemed too much of an addition as it can be achieved atm with this
local Nested = {}
Nested.__index = Nested
function Nested:__tojson()
return {
name= self.name,
child= if self.child then self.child:__tojson() else nil
}
end
function Nested.new(name,parent)
return setmetatable({
name=name,
parent=parent,
child=nil
},Nested)
end
local root = Nested.new("root")
root.child = Nested.new("child1",root)
root.child.child = Nested.new("child2",root.child)
ll.OwnerSay(lljson.encode(root:__tojson()))
Log In
H
Harold Linden
We have a fix that allows things like
local ReturnVecJsonMeta = {}
function ReturnVecJsonMeta:__tojson()
return self.foo
end
local ReturnTableJsonMeta = {}
function ReturnTableJsonMeta:__tojson()
return {self.foo, "baz"}
end
local sampleObj = {
foo=vector(1, 2, 3),
bar="ignored",
}
assert(lljson.encode(setmetatable(sampleObj, ReturnVecJsonMeta)), '"<1,2,3>"')
assert(lljson.encode(setmetatable(sampleObj, ReturnTableJsonMeta)), '["<1,2,3>","baz"]')
that will go out in a future update.
H
Harold Linden
inĀ progress
H
Harold Linden
planned
Thanks for sending this in! Yep, we'll get to this soonish.