Update EncodeJson in redbean to ignore useoutput outside of request handling

This commit is contained in:
Paul Kulchenko 2021-11-11 15:23:33 -08:00
parent 0793d9cf80
commit 7e1055fdb6
2 changed files with 10 additions and 4 deletions

View file

@ -508,10 +508,15 @@ FUNCTIONS
encodebase64.c.
EncodeJson(value[,options:table]) → json:str
Turns passed Lua value into a JSON string. The following options
can be used:
Turns passed Lua value into a JSON string. Tables with non-zero
length (as reported by `#`) are encoded as arrays with non-array
elements ignored. Empty tables are encoded as empty arrays. All
other tables are encoded as objects with numerical keys
converted to strings (so `{[3]=1}` is encoded as `{"3":1}`).
The following options can be used:
- useoutput: (bool=false) encodes the result directly to the
output buffer and returns `nil` value.
output buffer and returns `nil` value. This option is
ignored if used outside of request handling code.
- numformat: (string="%.14g") sets numeric format to be used.
- maxdepth: (number=64) sets the max number of nested tables.

View file

@ -4764,7 +4764,8 @@ static int LuaEncodeJson(lua_State *L) {
if (lua_istable(L, 2)) {
lua_settop(L, 2); // discard any extra arguments
lua_getfield(L, 2, "useoutput");
if (lua_isboolean(L, -1)) useoutput = lua_toboolean(L, -1);
// ignore useoutput outside of request handling
if (ishandlingrequest && lua_isboolean(L, -1)) useoutput = lua_toboolean(L, -1);
lua_getfield(L, 2, "maxdepth");
maxdepth = luaL_optinteger(L, -1, maxdepth);
lua_getfield(L, 2, "numformat");