From 227dd97036ded8a3caeb491f80f2c257574eafe8 Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Fri, 11 Aug 2023 14:48:13 -0400 Subject: [PATCH] m-lua-scripting: always parse JSON to String if its type is not recognized This allows parsing JSON strings without quotes. --- modules/module-lua-scripting/api/json.c | 14 +++++++------- tests/wplua/scripts/json.lua | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/modules/module-lua-scripting/api/json.c b/modules/module-lua-scripting/api/json.c index cae96af8..83074576 100644 --- a/modules/module-lua-scripting/api/json.c +++ b/modules/module-lua-scripting/api/json.c @@ -123,13 +123,6 @@ push_luajson (lua_State *L, WpSpaJson *json) lua_pushnumber (L, value); } - /* String */ - else if (wp_spa_json_is_string (json)) { - g_autofree gchar *value = wp_spa_json_parse_string (json); - g_warn_if_fail (value); - lua_pushstring (L, value); - } - /* Array */ else if (wp_spa_json_is_array (json)) { g_auto (GValue) item = G_VALUE_INIT; @@ -163,6 +156,13 @@ push_luajson (lua_State *L, WpSpaJson *json) lua_setfield (L, -2, key_str); } } + + /* Otherwise alwyas parse as String to allow parsing strings without quotes */ + else { + g_autofree gchar *value = wp_spa_json_parse_string (json); + g_warn_if_fail (value); + lua_pushstring (L, value); + } } static int diff --git a/tests/wplua/scripts/json.lua b/tests/wplua/scripts/json.lua index 3671a698..c0b5d638 100644 --- a/tests/wplua/scripts/json.lua +++ b/tests/wplua/scripts/json.lua @@ -169,6 +169,14 @@ val = json:parse () assert (val[1] == "foo") assert (val[2] == "bar") +json = Json.Raw ("[foo, bar]") +assert (json:is_array()) +assert (json:get_data() == "[foo, bar]") +assert (json:get_data() == json:to_string()) +val = json:parse () +assert (val[1] == "foo") +assert (val[2] == "bar") + json = Json.Raw ("{\"name\": \"wireplumber\", \"version\": [0, 4, 7]}") assert (json:is_object()) assert (json:get_data() == "{\"name\": \"wireplumber\", \"version\": [0, 4, 7]}")