From: James Prestwood <prestwoj at gmail.com>
To: iwd at lists.01.org
Subject: [PATCH 6/8] json: support arrays of objects
Date: Wed, 05 Jan 2022 10:46:25 -0800 [thread overview]
Message-ID: <20220105184627.329505-6-prestwoj@gmail.com> (raw)
In-Reply-To: 20220105184627.329505-1-prestwoj@gmail.com
[-- 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
reply other threads:[~2022-01-05 18:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220105184627.329505-6-prestwoj@gmail.com \
--to=unknown@example.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.