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

@ -42,10 +42,8 @@ static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags
spa_pod_builder_push_object(b, &f[0], info->parent, id);
spa_json_enter(iter, &it[0]);
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
while ((l = spa_json_object_next(&it[0], key, sizeof(key), &v)) > 0) {
const struct spa_type_info *pi;
if ((l = spa_json_next(&it[0], &v)) <= 0)
break;
if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
type = pi->type;
else if (!spa_atou32(key, &type, 0))

View file

@ -39,7 +39,7 @@ struct spa_json {
uint32_t depth;
};
#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), 0, 0, 0 })
#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), NULL, 0, 0 })
static inline void spa_json_init(struct spa_json * iter, const char *data, size_t size)
{
@ -54,6 +54,8 @@ static inline void spa_json_enter(struct spa_json * iter, struct spa_json * sub)
#define SPA_JSON_SAVE(iter) ((struct spa_json) { (iter)->cur, (iter)->end, NULL, (iter)->state, 0 })
#define SPA_JSON_START(iter,p) ((struct spa_json) { (p), (iter)->end, NULL, 0, 0 })
/** Get the next token. \a value points to the token and the return value
* is the length. Returns -1 on parse error, 0 on end of input. */
static inline int spa_json_next(struct spa_json * iter, const char **value)
@ -594,7 +596,7 @@ static inline int spa_json_parse_stringn(const char *val, int len, char *result,
{
const char *p;
if (maxlen <= len)
return -1;
return -ENOSPC;
if (!spa_json_is_string(val, len)) {
if (result != val)
memmove(result, val, len);
@ -712,6 +714,19 @@ static inline int spa_json_encode_string(char *str, int size, const char *val)
return len-1;
}
static inline int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
{
int res1, res2;
while (true) {
res1 = spa_json_get_string(iter, key, maxkeylen);
if (res1 <= 0 && res1 != -ENOSPC)
return res1;
res2 = spa_json_next(iter, value);
if (res2 <= 0 || res1 != -ENOSPC)
return res2;
}
}
/**
* \}
*/