m-lua-scripting: always parse JSON to String if its type is not recognized

This allows parsing JSON strings without quotes.
This commit is contained in:
Julian Bouzas
2023-08-11 14:48:13 -04:00
parent e24e52c6d3
commit 227dd97036
2 changed files with 15 additions and 7 deletions

View File

@@ -123,13 +123,6 @@ push_luajson (lua_State *L, WpSpaJson *json)
lua_pushnumber (L, value); 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 */ /* Array */
else if (wp_spa_json_is_array (json)) { else if (wp_spa_json_is_array (json)) {
g_auto (GValue) item = G_VALUE_INIT; g_auto (GValue) item = G_VALUE_INIT;
@@ -163,6 +156,13 @@ push_luajson (lua_State *L, WpSpaJson *json)
lua_setfield (L, -2, key_str); 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 static int

View File

@@ -169,6 +169,14 @@ val = json:parse ()
assert (val[1] == "foo") assert (val[1] == "foo")
assert (val[2] == "bar") 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]}") json = Json.Raw ("{\"name\": \"wireplumber\", \"version\": [0, 4, 7]}")
assert (json:is_object()) assert (json:is_object())
assert (json:get_data() == "{\"name\": \"wireplumber\", \"version\": [0, 4, 7]}") assert (json:get_data() == "{\"name\": \"wireplumber\", \"version\": [0, 4, 7]}")