lunajson
lunajson copied to clipboard
Expose encoder dispatcher for modification
Expose the dispatcher table to allow custom encoding of data types.
Example use case:
local zero_based_meta = {
__index = function(t, k)
if type(k) == "number" then
return t.raw[k + 1]
end
end
}
local encode, dispatcher = newencoder()
local f_table = dispatcher.table
function dispatcher.table(o)
return f_table(getmetatable(o) == zero_based_meta and o.raw or o)
end
local arr = setmetatable({ raw = {3,7,4} }, zero_based_meta)
print(arr[2]) --> 4
print(encode(arr)) --> [3, 7, 4]
Hello, Take care about possible non-numeric key I think about the potential new "n" field.
I suppose a broken example doesn't serve its purpose very well. Fixed!