lua-cjson icon indicating copy to clipboard operation
lua-cjson copied to clipboard

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

Open drmingdrmer opened this issue 10 years ago • 5 comments

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) -- > "[]"

drmingdrmer avatar Jul 16 '15 03:07 drmingdrmer

:+1:

nijikokun avatar Jul 24 '15 03:07 nijikokun

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

ppwfx avatar Feb 07 '17 14:02 ppwfx

any progress for this PR?

moooofly avatar Aug 21 '19 01:08 moooofly

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

drmingdrmer avatar Aug 22 '19 09:08 drmingdrmer

@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.

moooofly avatar Aug 23 '19 01:08 moooofly