Serialise Lua data into JSON string. */
| 845 | |
| 846 | /* Serialise Lua data into JSON string. */ |
| 847 | static void json_append_data(lua_State *l, json_config_t *cfg, |
| 848 | int current_depth, strbuf_t *json) |
| 849 | { |
| 850 | int len; |
| 851 | int as_array = 0; |
| 852 | int has_metatable; |
| 853 | |
| 854 | switch (lua_type(l, -1)) { |
| 855 | case LUA_TSTRING: |
| 856 | json_append_string(l, json, -1); |
| 857 | break; |
| 858 | case LUA_TNUMBER: |
| 859 | json_append_number(l, cfg, json, -1); |
| 860 | break; |
| 861 | case LUA_TBOOLEAN: |
| 862 | if (lua_toboolean(l, -1)) |
| 863 | strbuf_append_mem(json, "true", 4); |
| 864 | else |
| 865 | strbuf_append_mem(json, "false", 5); |
| 866 | break; |
| 867 | case LUA_TTABLE: |
| 868 | current_depth++; |
| 869 | json_check_encode_depth(l, cfg, current_depth, json); |
| 870 | |
| 871 | has_metatable = lua_getmetatable(l, -1); |
| 872 | |
| 873 | if (has_metatable) { |
| 874 | lua_pushlightuserdata(l, json_lightudata_mask(&json_array)); |
| 875 | lua_rawget(l, LUA_REGISTRYINDEX); |
| 876 | as_array = lua_rawequal(l, -1, -2); |
| 877 | lua_pop(l, 2); |
| 878 | } |
| 879 | |
| 880 | if (as_array) { |
| 881 | len = lua_objlen(l, -1); |
| 882 | json_append_array(l, cfg, current_depth, json, len); |
| 883 | } else { |
| 884 | len = lua_array_length(l, cfg, json); |
| 885 | |
| 886 | if (len > 0 || (len == 0 && !cfg->encode_empty_table_as_object)) { |
| 887 | json_append_array(l, cfg, current_depth, json, len); |
| 888 | } else { |
| 889 | if (has_metatable) { |
| 890 | lua_getmetatable(l, -1); |
| 891 | lua_pushlightuserdata(l, json_lightudata_mask( |
| 892 | &json_empty_array)); |
| 893 | lua_rawget(l, LUA_REGISTRYINDEX); |
| 894 | as_array = lua_rawequal(l, -1, -2); |
| 895 | lua_pop(l, 2); /* pop pointer + metatable */ |
| 896 | if (as_array) { |
| 897 | json_append_array(l, cfg, current_depth, json, 0); |
| 898 | break; |
| 899 | } |
| 900 | } |
| 901 | json_append_object(l, cfg, current_depth, json); |
| 902 | } |
| 903 | } |
| 904 | break; |
no test coverage detected