mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
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:
committed by
Christian Decker
parent
7b59e26dd7
commit
82ff580a66
@@ -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--;
|
||||
|
||||
Reference in New Issue
Block a user