Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:ecsos
wireplumber
0001-m-lua-scripting-ignore-string-integer-tabl...
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0001-m-lua-scripting-ignore-string-integer-table-keys-when-constructing-a-JSON-Array-Object.patch of Package wireplumber
From 6b761c03e18b89c5121e4f48ce7df471504801fd Mon Sep 17 00:00:00 2001 From: Julian Bouzas <julian.bouzas@collabora.com> Date: Wed, 4 Jan 2023 11:20:14 -0500 Subject: [PATCH] m-lua-scripting: ignore string/integer table keys when constructing a JSON Array/Object --- modules/module-lua-scripting/api/json.c | 97 +++++++++++++------------ tests/wplua/scripts/json.lua | 26 +++++++ 2 files changed, 77 insertions(+), 46 deletions(-) diff --git a/modules/module-lua-scripting/api/json.c b/modules/module-lua-scripting/api/json.c index be12ea38..cae96af8 100644 --- a/modules/module-lua-scripting/api/json.c +++ b/modules/module-lua-scripting/api/json.c @@ -242,31 +242,33 @@ spa_json_array_new (lua_State *L) luaL_checktype (L, 1, LUA_TTABLE); lua_pushnil (L); - while (lua_next (L, 1)) { - switch (lua_type (L, -1)) { - case LUA_TBOOLEAN: - wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); - break; - case LUA_TNUMBER: - if (lua_isinteger (L, -1)) - wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); - else - wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); - break; - case LUA_TSTRING: - wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); - break; - case LUA_TUSERDATA: { - WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); - wp_spa_json_builder_add_json (builder, json); - break; + while (lua_next (L, -2)) { + /* We only add table values with integer keys */ + if (lua_isinteger (L, -2)) { + switch (lua_type (L, -1)) { + case LUA_TBOOLEAN: + wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); + break; + case LUA_TNUMBER: + if (lua_isinteger (L, -1)) + wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); + else + wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); + break; + case LUA_TSTRING: + wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); + break; + case LUA_TUSERDATA: { + WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); + wp_spa_json_builder_add_json (builder, json); + break; + } + default: + luaL_error (L, "Json does not support lua type ", + lua_typename(L, lua_type(L, -1))); + break; } - default: - luaL_error (L, "Json does not support lua type ", - lua_typename(L, lua_type(L, -1))); - break; } - lua_pop (L, 1); } @@ -285,30 +287,33 @@ spa_json_object_new (lua_State *L) lua_pushnil (L); while (lua_next (L, -2)) { - wp_spa_json_builder_add_property (builder, lua_tostring (L, -2)); - - switch (lua_type (L, -1)) { - case LUA_TBOOLEAN: - wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); - break; - case LUA_TNUMBER: - if (lua_isinteger (L, -1)) - wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); - else - wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); - break; - case LUA_TSTRING: - wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); - break; - case LUA_TUSERDATA: { - WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); - wp_spa_json_builder_add_json (builder, json); - break; + /* We only add table values with string keys */ + if (lua_type (L, -2) == LUA_TSTRING) { + wp_spa_json_builder_add_property (builder, lua_tostring (L, -2)); + + switch (lua_type (L, -1)) { + case LUA_TBOOLEAN: + wp_spa_json_builder_add_boolean (builder, lua_toboolean (L, -1)); + break; + case LUA_TNUMBER: + if (lua_isinteger (L, -1)) + wp_spa_json_builder_add_int (builder, lua_tointeger (L, -1)); + else + wp_spa_json_builder_add_float (builder, lua_tonumber (L, -1)); + break; + case LUA_TSTRING: + wp_spa_json_builder_add_string (builder, lua_tostring (L, -1)); + break; + case LUA_TUSERDATA: { + WpSpaJson *json = wplua_checkboxed (L, -1, WP_TYPE_SPA_JSON); + wp_spa_json_builder_add_json (builder, json); + break; + } + default: + luaL_error (L, "Json does not support lua type ", + lua_typename(L, lua_type(L, -1))); + break; } - default: - luaL_error (L, "Json does not support lua type ", - lua_typename(L, lua_type(L, -1))); - break; } lua_pop (L, 1); diff --git a/tests/wplua/scripts/json.lua b/tests/wplua/scripts/json.lua index e01d7f6f..3671a698 100644 --- a/tests/wplua/scripts/json.lua +++ b/tests/wplua/scripts/json.lua @@ -102,6 +102,19 @@ assert (json:is_array()) assert (json:get_data() == "[[{\"key1\":1}, {\"key2\":2}]]") assert (json:get_data() == json:to_string()) +table = {} +table[1] = 1 +table[2] = 2 +table[3] = 3 +table["4"] = 4 +json = Json.Array (table) +assert (json:is_array()) +val = json:parse () +assert (val[1] == 1) +assert (val[2] == 2) +assert (val[3] == 3) +assert (val["4"] == nil) + -- Object json = Json.Object { key1 = Json.Null(), @@ -134,6 +147,19 @@ assert (val.key7.key_nested3[2]) assert (not val.key7.key_nested3[3]) assert (val.key7["Key with spaces and (special % characters)"] == 50.0) +table = {} +table["1"] = 1 +table["2"] = 2 +table["3"] = 3 +table[4] = 4 +json = Json.Object (table) +assert (json:is_object()) +val = json:parse () +assert (val["1"] == 1) +assert (val["2"] == 2) +assert (val["3"] == 3) +assert (val[4] == nil) + -- Raw json = Json.Raw ("[\"foo\", \"bar\"]") assert (json:is_array()) -- GitLab
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor