json: add more efficient iterators for objects and arrays.

Christian points out that we can iterate by ->size rather than calling
json_next() to find the end (which traverses the entire object!).

Now ->size is reliable (since previous patch), this is OK.

Reported-by: @cdecker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-01-17 12:42:13 +10:30
committed by Christian Decker
parent 7b59e26dd7
commit 82ff580a66
7 changed files with 97 additions and 101 deletions

View File

@@ -180,13 +180,13 @@ const jsmntok_t *json_next(const jsmntok_t *tok)
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
const char *label)
{
const jsmntok_t *t, *end;
const jsmntok_t *t;
size_t i;
if (tok->type != JSMN_OBJECT)
return NULL;
end = json_next(tok);
for (t = tok + 1; t < end; t = json_next(t+1))
json_for_each_obj(i, t, tok)
if (json_tok_streq(buffer, t, label))
return t + 1;
@@ -195,13 +195,13 @@ const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
{
const jsmntok_t *t, *end;
const jsmntok_t *t;
size_t i;
if (tok->type != JSMN_ARRAY)
return NULL;
end = json_next(tok);
for (t = tok + 1; t < end; t = json_next(t)) {
json_for_each_arr(i, t, tok) {
if (index == 0)
return t;
index--;