-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Labels
bugSomething isn't workingSomething isn't working
Description
local tab = { 1, k ="val" }
setmetatable(tab, lljson.empty_array_mt)
print(lljson.encode(tab))
--> [1]I expected empty_array_mt to work only on empty tables but it always encodes an array, in practice doing the same than array_mt.
It seems that it's not checking for the length:
slua/VM/src/cjson/lua_cjson.cpp
Lines 1182 to 1201 in bdd984b
| len = lua_array_length(l, cfg, json); | |
| if (len > 0 || (len == 0 && !cfg->encode_empty_table_as_object)) { | |
| json_append_array(l, cfg, current_depth, json, len, raw); | |
| } else { | |
| if (has_metatable) { | |
| lua_getmetatable(l, -1); | |
| lua_pushlightuserdatatagged(l, json_lightudata_mask(JSON_EMPTY_ARRAY), LU_TAG_JSON_INTERNAL); | |
| lua_rawget(l, LUA_REGISTRYINDEX); | |
| as_array = lua_rawequal(l, -1, -2); | |
| lua_pop(l, 2); /* pop pointer + metatable */ | |
| if (as_array) { | |
| len = lua_objlen(l, -1); | |
| raw = 1; | |
| json_append_array(l, cfg, current_depth, json, len, raw); | |
| break; | |
| } | |
| } | |
| json_append_object(l, cfg, current_depth, json); | |
| } |
len seems to be -1 in this case, perhaps this would work?
if (len == 0 && has_metatable) {Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working