* [PATCH 6/8] json: support arrays of objects
@ 2022-01-05 18:46 James Prestwood
0 siblings, 0 replies; only message in thread
From: James Prestwood @ 2022-01-05 18:46 UTC (permalink / raw)
To: iwd
[-- Attachment #1: Type: text/plain, Size: 2731 bytes --]
---
src/json.c | 39 +++++++++++++++++++++++++++++++++++++--
src/json.h | 2 ++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/json.c b/src/json.c
index 4e4c004e..d38d884e 100644
--- a/src/json.c
+++ b/src/json.c
@@ -401,6 +401,21 @@ bool json_iter_get_null(struct json_iter *iter)
return false;
}
+bool json_iter_get_container(struct json_iter *iter,
+ struct json_iter *container)
+{
+ struct json_contents *c = iter->contents;
+ jsmntok_t *t = c->tokens + iter->current;
+
+ /* TODO: Add nested array support */
+ if (t->type != JSMN_OBJECT)
+ return false;
+
+ iter_recurse(iter, t, container);
+
+ return true;
+}
+
enum json_type json_iter_get_type(struct json_iter *iter)
{
struct json_contents *c = iter->contents;
@@ -412,15 +427,35 @@ enum json_type json_iter_get_type(struct json_iter *iter)
bool json_iter_next(struct json_iter *iter)
{
struct json_contents *c = iter->contents;
+ jsmntok_t *t = c->tokens + iter->current;
+ int inc = 1;
/* For now only allow json_iter_next() on arrays */
if (!iter->array)
return false;
- if (c->tokens + iter->current + 1 > ITER_END(iter))
+ /*
+ * If this is the initial iteration skip this and just increment
+ * current by 1 since this iterator points to an array which needs to
+ * be advanced to the first token in the array.
+ *
+ * In addition primitive types and empty object will have a size of 1,
+ * so no special handling is needed there.
+ *
+ * For objects (and arrays in the future), 'current' needs to be
+ * advanced by all objects child tokens plus the object token itself.
+ * This check ensures:
+ * 1. It is not the initial iteration
+ * 2. This is an object
+ * 3. The object is not empty
+ */
+ if (iter->current != iter->start && t->type == JSMN_OBJECT && t->size)
+ inc = find_parent_tokens(iter, t) + 1;
+
+ if (c->tokens + iter->current + inc > ITER_END(iter))
return false;
- iter->current++;
+ iter->current += inc;
/* TODO: Add support for nested array iteration */
if ((c->tokens + iter->current)->type == JSMN_ARRAY)
diff --git a/src/json.h b/src/json.h
index 75ac9853..c7993a5a 100644
--- a/src/json.h
+++ b/src/json.h
@@ -94,6 +94,8 @@ bool json_iter_get_int(struct json_iter *iter, int *i);
bool json_iter_get_uint(struct json_iter *iter, unsigned int *i);
bool json_iter_get_boolean(struct json_iter *iter, bool *b);
bool json_iter_get_null(struct json_iter *iter);
+bool json_iter_get_container(struct json_iter *iter,
+ struct json_iter *container);
enum json_type json_iter_get_type(struct json_iter *iter);
bool json_iter_next(struct json_iter *iter);
--
2.31.1
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2022-01-05 18:46 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-05 18:46 [PATCH 6/8] json: support arrays of objects James Prestwood
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox