| 2792 | } |
| 2793 | |
| 2794 | static const char *tojsonstring_with_nested(lua_State *L, int idx, size_t *len, std::list<const void*> &jsons) |
| 2795 | { |
| 2796 | const void *addr = lua_topointer(L, idx); |
| 2797 | if (nullptr != addr && glua::util::find(jsons.begin(), jsons.end(), addr)) |
| 2798 | { |
| 2799 | lua_pushfstring(L, "%p", addr); |
| 2800 | return lua_tolstring(L, -1, len); |
| 2801 | } |
| 2802 | if (!luaL_callmeta(L, idx, "__tojsonstring")) { /* no metafield? */ |
| 2803 | switch (lua_type(L, idx)) { |
| 2804 | case LUA_TNUMBER: { |
| 2805 | if (lua_isinteger(L, idx)) |
| 2806 | lua_pushfstring(L, "%I", lua_tointeger(L, idx)); |
| 2807 | else |
| 2808 | lua_pushfstring(L, "%f", lua_tonumber(L, idx)); |
| 2809 | break; |
| 2810 | } |
| 2811 | case LUA_TSTRING: |
| 2812 | lua_pushvalue(L, idx); |
| 2813 | break; |
| 2814 | case LUA_TBOOLEAN: |
| 2815 | lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); |
| 2816 | break; |
| 2817 | case LUA_TNIL: |
| 2818 | lua_pushliteral(L, "nil"); |
| 2819 | break; |
| 2820 | case LUA_TTABLE: |
| 2821 | { |
| 2822 | jsons.push_back(addr); |
| 2823 | GluaTableMapP map = luaL_create_lua_table_map_in_memory_pool(L); |
| 2824 | luaL_traverse_table_with_nested(L, idx, lua_table_to_map_traverser_with_nested, map, jsons, 0); |
| 2825 | std::stringstream ss; |
| 2826 | luatablemap_to_json_stream(map, ss); |
| 2827 | if (len) |
| 2828 | *len = ss.str().size(); |
| 2829 | lua_pushlstring(L, ss.str().c_str(), ss.str().size()); |
| 2830 | } |
| 2831 | break; |
| 2832 | default: |
| 2833 | lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), |
| 2834 | (intptr_t) 0); |
| 2835 | //lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), |
| 2836 | // lua_topointer(L, idx)); |
| 2837 | break; |
| 2838 | } |
| 2839 | } |
| 2840 | return lua_tolstring(L, -1, len); |
| 2841 | } |
| 2842 | |
| 2843 | LUALIB_API const char *(luaL_tojsonstring)(lua_State *L, int idx, size_t *len) |
| 2844 | { |
no test coverage detected