mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 13:52:28 +00:00
Further improve json / lua serialization
This commit is contained in:
parent
9e86980191
commit
ee82cee432
10 changed files with 248 additions and 155 deletions
17
third_party/lua/luaencodejsondata.c
vendored
17
third_party/lua/luaencodejsondata.c
vendored
|
@ -99,12 +99,17 @@ static int LuaEncodeJsonDataImpl(lua_State *L, char **buf, int level,
|
|||
// json tables must be arrays or use string keys
|
||||
goto OnError;
|
||||
}
|
||||
RETURN_ON_ERROR(sli = AppendStrList(&sl));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -2, visited));
|
||||
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -1, visited));
|
||||
// the json parser inserts a `__json_object__` into empty
|
||||
// objects, so we don't serialize `{}` as `[]` by mistake
|
||||
// and as such, we should ignore it here, for readability
|
||||
if (strcmp(luaL_checkstring(L, -2), "__json_object__")) {
|
||||
RETURN_ON_ERROR(sli = AppendStrList(&sl));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -2, visited));
|
||||
RETURN_ON_ERROR(appendw(&sl.p[sli], ':'));
|
||||
RETURN_ON_ERROR(LuaEncodeJsonDataImpl(L, &sl.p[sli], level - 1,
|
||||
numformat, -1, visited));
|
||||
}
|
||||
lua_pop(L, 1); // table/-2, key/-1
|
||||
}
|
||||
// stack: table/-1, as the key was popped by lua_next
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue