Improve Lua and JSON serialization

This commit is contained in:
Justine Tunney 2022-07-12 23:31:06 -07:00
parent 3027d67037
commit e3cd476a9b
20 changed files with 1041 additions and 476 deletions

View file

@ -4221,11 +4221,11 @@ static int LuaLog(lua_State *L) {
}
static int LuaEncodeSmth(lua_State *L,
int Encoder(lua_State *, char **, char *, int)) {
int useoutput = false;
int maxdepth = 64;
char *numformat = "%.14g";
int Encoder(lua_State *, char **, int, bool)) {
char *p = 0;
int maxdepth = 64;
int sorted = true;
int useoutput = false;
if (lua_istable(L, 2)) {
lua_settop(L, 2); // discard any extra arguments
lua_getfield(L, 2, "useoutput");
@ -4233,11 +4233,11 @@ static int LuaEncodeSmth(lua_State *L,
if (ishandlingrequest && lua_isboolean(L, -1)) {
useoutput = lua_toboolean(L, -1);
}
lua_getfield(L, 2, "numformat");
numformat = luaL_optstring(L, -1, numformat);
lua_getfield(L, 2, "sorted");
sorted = lua_toboolean(L, -1);
}
lua_settop(L, 1); // keep the passed argument on top
if (Encoder(L, useoutput ? &outbuf : &p, numformat, -1) == -1) {
if (Encoder(L, useoutput ? &outbuf : &p, -1, sorted) == -1) {
free(p);
return 2;
}
@ -5352,7 +5352,7 @@ static void LuaPrint(lua_State *L) {
if (n > 0) {
for (i = 1; i <= n; i++) {
if (i > 1) appendw(&b, '\t');
LuaEncodeLuaData(L, &b, "g", i);
LuaEncodeLuaData(L, &b, i, true);
}
appendw(&b, '\n');
WRITE(1, b, appendz(b).i);