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

@ -3460,20 +3460,15 @@ 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, kl = strlen(key) + 3;
char k[kl];
if (spa_json_begin_object(&it[0], obj, strlen(obj)) <= 0)
return -EINVAL;
while (spa_json_get_string(&it[0], k, sizeof(k)) > 0) {
if (spa_streq(k, key)) {
if (spa_json_get_string(&it[0], value, len) <= 0)
continue;
return 0;
} else {
if (spa_json_next(&it[0], &v) <= 0)
break;
}
while ((l = spa_json_object_next(&it[0], k, kl, &v)) > 0) {
if (spa_streq(k, key))
return spa_json_parse_stringn(v, l, value, len);
}
return -ENOENT;
}