spa: add spa_json_object_next

This gets the next key and value from an object. This function is better
because it will skip key/value pairs that don't fit in the array to hold
the key.

The previous code patter would stop parsing the object as soon as a key
larger than the available space was found.
This commit is contained in:
Wim Taymans 2024-09-13 16:26:36 +02:00
parent cd81b5f39a
commit ce390d5b22
24 changed files with 171 additions and 269 deletions

View file

@ -80,14 +80,10 @@ static int parse_cmd(void *user_data, const char *location,
while (spa_json_enter_object(&it[0], &it[1]) > 0) {
char *cmd = NULL, *args = NULL, *flags = NULL;
const char *val;
int len;
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
const char *val;
int len;
if ((len = spa_json_next(&it[1], &val)) <= 0)
break;
while ((len = spa_json_object_next(&it[1], key, sizeof(key), &val)) > 0) {
if (spa_streq(key, "cmd")) {
cmd = (char*)val;
spa_json_parse_stringn(val, len, cmd, len+1);
@ -99,6 +95,8 @@ static int parse_cmd(void *user_data, const char *location,
len = spa_json_container_len(&it[1], val, len);
flags = (char*)val;
spa_json_parse_stringn(val, len, flags, len+1);
} else {
pw_log_warn("unknown pulse.cmd key %s", key);
}
}
if (cmd != NULL)
@ -106,7 +104,6 @@ static int parse_cmd(void *user_data, const char *location,
if (res < 0)
break;
}
return res;
}

View file

@ -185,6 +185,7 @@ static int do_extension_stream_restore_read(struct module *module, struct client
struct volume vol = VOLUME_INIT;
struct channel_map map = CHANNEL_MAP_INIT;
float volume = 0.0f;
int len;
if (key_to_name(item->key, name, sizeof(name)) < 0)
continue;
@ -194,29 +195,31 @@ static int do_extension_stream_restore_read(struct module *module, struct client
if (spa_json_begin_object(&it[0], item->value, strlen(item->value)) <= 0)
continue;
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
while ((len = spa_json_object_next(&it[0], key, sizeof(key), &value)) > 0) {
if (spa_streq(key, "volume")) {
if (spa_json_get_float(&it[0], &volume) <= 0)
if (spa_json_parse_float(value, len, &volume) <= 0)
continue;
}
else if (spa_streq(key, "mute")) {
if (spa_json_get_bool(&it[0], &mute) <= 0)
if (spa_json_parse_bool(value, len, &mute) <= 0)
continue;
}
else if (spa_streq(key, "volumes")) {
vol = VOLUME_INIT;
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
if (!spa_json_is_array(value, len))
continue;
spa_json_enter(&it[0], &it[1]);
for (vol.channels = 0; vol.channels < CHANNELS_MAX; vol.channels++) {
if (spa_json_get_float(&it[1], &vol.values[vol.channels]) <= 0)
break;
}
}
else if (spa_streq(key, "channels")) {
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
if (!spa_json_is_array(value, len))
continue;
spa_json_enter(&it[0], &it[1]);
for (map.channels = 0; map.channels < CHANNELS_MAX; map.channels++) {
char chname[16];
if (spa_json_get_string(&it[1], chname, sizeof(chname)) <= 0)
@ -225,11 +228,9 @@ static int do_extension_stream_restore_read(struct module *module, struct client
}
}
else if (spa_streq(key, "target-node")) {
if (spa_json_get_string(&it[0], device_name, sizeof(device_name)) <= 0)
if (spa_json_parse_stringn(value, len, device_name, sizeof(device_name)) <= 0)
continue;
}
else if (spa_json_next(&it[0], &value) <= 0)
break;
}
message_put(reply,
TAG_STRING, name,

View file

@ -933,18 +933,16 @@ static int json_object_find(const char *obj, const char *key, char *value, size_
struct spa_json it[1];
const char *v;
char k[128];
int l;
if (spa_json_begin_object(&it[0], obj, strlen(obj)) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[0], k, sizeof(k)) > 0) {
while ((l = spa_json_object_next(&it[0], k, sizeof(k), &v)) > 0) {
if (spa_streq(k, key)) {
if (spa_json_get_string(&it[0], value, len) <= 0)
if (spa_json_parse_stringn(v, l, value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[0], &v) <= 0)
break;
}
}
return -ENOENT;

View file

@ -997,10 +997,7 @@ int servers_create_and_start(struct impl *impl, const char *addresses, struct pw
if (spa_json_is_object(v, len)) {
spa_json_enter(&it[0], &it[1]);
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
if ((len = spa_json_next(&it[1], &v)) <= 0)
break;
while ((len = spa_json_object_next(&it[1], key, sizeof(key), &v)) > 0) {
if (spa_streq(key, "address")) {
spa_json_parse_stringn(v, len, addr_str, sizeof(addr_str));
} else if (spa_streq(key, "max-clients")) {