Skip to content

Conversation

Ayplow
Copy link

@Ayplow Ayplow commented Aug 5, 2019

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]

@tst2005
Copy link
Contributor

tst2005 commented Aug 6, 2019

Hello,
Take care about possible non-numeric key
I think about the potential new "n" field.

@Ayplow
Copy link
Author

Ayplow commented Aug 6, 2019

I suppose a broken example doesn't serve its purpose very well. Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants