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

option to force to encode empty table to "[]" #25

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

Conversation

drmingdrmer
Copy link

Since lua-cjson use table index type to decide the result json type that a lua table is
encoded to, there is no way to encode empty lua table to a list in json.

This patch solves this problem by assigning a special meta-table to an
empty table to force it to be encoded to "[]" instead of "{}":

local t = {}
setmetatable(t,cjson.as_array)
json.encode(t) -- > "[]"

Since lua-cjson use table index type to decide what type a lua table is
encoded to, there is no way to encode empty lua type to list.

This patch solves this problem by assigning a special meta-table to an
empty table to force it to be encoded to "[]" instead of "{}":

    setmetatable({},cjson.as_array)
@nijikokun
Copy link

👍

@ppwfx
Copy link

ppwfx commented Feb 7, 2017

Why is it still open? I need this feature and don't want to fork it..
Is there a workaround for this?

@moooofly
Copy link

any progress for this PR?

@drmingdrmer
Copy link
Author

@moooofly It seems the author has no intent to merge it. :(

@moooofly
Copy link

@drmingdrmer
anyway, this PR helps me a lot, I've patched this code into https://github.com/antirez/redis/blob/unstable/deps/lua/src/lua_cjson.c , and works well.

Thanks a lot.

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.

4 participants