summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-04-12 15:30:17 +0200
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2024-04-12 15:30:17 +0200
commitc0bcb33d99ff939eb75758f80c948a10ea6733d2 (patch)
tree35c5388f77e102de4e05a17e0658027246b153b8
parent1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e (diff)
json.h: avoid expanding `o` twiceHEADmain
Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
-rw-r--r--include/json.h11
1 files changed, 8 insertions, 3 deletions
diff --git a/include/json.h b/include/json.h
index 85749d6..c8c844e 100644
--- a/include/json.h
+++ b/include/json.h
@@ -129,9 +129,6 @@ struct json *json_bool(bool boolean);
char *json_print(struct json *json);
-#define json_foreach(e, o) \
- for (struct json *e = json_check_type((o), JSON_OBJECT | JSON_ARRAY) ? (o)->children->items[0] : NULL; e; e = e->next)
-
static inline bool json_check_type(const struct json *json, const enum json_type type) {
return json && json->type & type;
}
@@ -160,4 +157,12 @@ static inline bool json_is_null(const struct json *json) {
return json && json->type == JSON_NULL;
}
+#define json_foreach(e, o) \
+ for (struct json *e = json_iter((o)); e; e = e->next)
+
+// Avoid expanding `o` twice in the foreach macro
+static inline struct json *json_iter(struct json *json) {
+ return json_check_type(json, JSON_OBJECT | JSON_ARRAY) ? json->children->items[0] : NULL;
+}
+
#endif