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
@@ -51,7 +51,7 @@ bool json_tok_is_num(const char *buffer, const jsmntok_t *tok);
|
||||
/* Is this the null primitive? */
|
||||
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);
|
||||
|
||||
/* Returns next token with same parent. */
|
||||
/* Returns next token with same parent (WARNING: slow!). */
|
||||
const jsmntok_t *json_next(const jsmntok_t *tok);
|
||||
|
||||
/* Get top-level member. */
|
||||
@@ -85,4 +85,13 @@ const jsmntok_t *json_delve(const char *buffer,
|
||||
const jsmntok_t *tok,
|
||||
const char *guide);
|
||||
|
||||
/* Iterator macro for array: i is counter, t is token ptr, arr is JSMN_ARRAY */
|
||||
#define json_for_each_arr(i, t, arr) \
|
||||
for (i = 0, t = (arr) + 1; i < (arr)->size; t = json_next(t), i++)
|
||||
|
||||
/* Iterator macro for object: i is counter, t is token ptr (t+1 is
|
||||
* contents of obj member), obj is JSMN_OBJECT */
|
||||
#define json_for_each_obj(i, t, obj) \
|
||||
for (i = 0, t = (obj) + 1; i < (obj)->size; t = json_next(t+1), i++)
|
||||
|
||||
#endif /* LIGHTNING_COMMON_JSON_H */
|
||||
|
||||
Reference in New Issue
Block a user