Skip to content

lljson.empty_array_mt behaves like lljson.array_mt #44

Description

@Suzanna-Linn
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:

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) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions