Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose encoder dispatcher for modification #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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