summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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