Fix oops w/ array serialization ordering

This commit is contained in:
Justine Tunney 2022-07-09 10:28:14 -07:00
parent 727d9cbf56
commit d37536bd4b
3 changed files with 5 additions and 5 deletions

View file

@ -54,17 +54,17 @@ assert(EncodeLatin1("helloÿÀ") == "hello\xff\xc0")
assert(EncodeLua(nil) == "nil") assert(EncodeLua(nil) == "nil")
assert(EncodeLua(0) == "0") assert(EncodeLua(0) == "0")
assert(EncodeLua(3.14) == "3.14") assert(EncodeLua(3.14) == "3.14")
assert(EncodeLua({2, 1}) == "{1, 2}") assert(EncodeLua({2, 1}) == "{2, 1}")
assert(EncodeJson(nil) == "null") assert(EncodeJson(nil) == "null")
assert(EncodeJson(0) == "0") assert(EncodeJson(0) == "0")
assert(EncodeJson(3.14) == "3.14") assert(EncodeJson(3.14) == "3.14")
assert(EncodeJson({2, 1}) == "[1,2]") assert(EncodeJson({2, 1}) == "[2,1]")
-- EncodeLua() permits serialization of cyclic data structures -- EncodeLua() permits serialization of cyclic data structures
x = {2, 1} x = {2, 1}
x[3] = x x[3] = x
assert(string.match(EncodeLua(x), "{\"cyclic@0x%x+\", 1, 2}")) assert(string.match(EncodeLua(x), "{2, 1, \"cyclic@0x%x+\"}"))
-- EncodeLua() sorts table entries -- EncodeLua() sorts table entries
x = {} x = {}

View file

@ -120,9 +120,9 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
lua_pop(L, 1); // table/-2, key/-1 lua_pop(L, 1); // table/-2, key/-1
} }
// stack: table/-1, as the key was popped by lua_next // stack: table/-1, as the key was popped by lua_next
SortStrList(&sl);
} }
RETURN_ON_ERROR(appendw(buf, isarray ? '[' : '{')); RETURN_ON_ERROR(appendw(buf, isarray ? '[' : '{'));
SortStrList(&sl);
RETURN_ON_ERROR(JoinStrList(&sl, buf, ',')); RETURN_ON_ERROR(JoinStrList(&sl, buf, ','));
FreeStrList(&sl); FreeStrList(&sl);
RETURN_ON_ERROR(appendw(buf, isarray ? ']' : '}')); RETURN_ON_ERROR(appendw(buf, isarray ? ']' : '}'));

View file

@ -181,8 +181,8 @@ static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
lua_pop(L, 1); // table/-2, key/-1 lua_pop(L, 1); // table/-2, key/-1
} }
lua_pop(L, 1); // table ref lua_pop(L, 1); // table ref
if (!isarray) SortStrList(&sl);
RETURN_ON_ERROR(appendw(buf, '{')); RETURN_ON_ERROR(appendw(buf, '{'));
SortStrList(&sl);
RETURN_ON_ERROR(JoinStrList(&sl, buf, READ16LE(", "))); RETURN_ON_ERROR(JoinStrList(&sl, buf, READ16LE(", ")));
RETURN_ON_ERROR(appendw(buf, '}')); RETURN_ON_ERROR(appendw(buf, '}'));
FreeStrList(&sl); FreeStrList(&sl);