diff --git a/tool/net/help.txt b/tool/net/help.txt index 2727494fa..01a4604ec 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -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. diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 1ba7ea713..615a8e4b9 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -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");