commit cc276e247179fb59c0dcd530250dd72fb8dd2b80
parent 81a09a04d650b8274745a56bb71ffe3d1fb81f62
Author: Joris Vink <joris@coders.se>
Date:   Thu,  8 Apr 2021 09:10:58 +0200
Add kore_json_item_attach().
Allows a JSON subtree to be engrafted after creation.
from Joel Arbring via patches@
Diffstat:
2 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/include/kore/kore.h b/include/kore/kore.h
@@ -1043,6 +1043,7 @@ void	kore_json_cleanup(struct kore_json *);
 void	kore_json_item_free(struct kore_json_item *);
 void	kore_json_init(struct kore_json *, const void *, size_t);
 void	kore_json_item_tobuf(struct kore_json_item *, struct kore_buf *);
+void	kore_json_item_attach(struct kore_json_item *, struct kore_json_item *);
 
 const char		*kore_json_strerror(void);
 struct kore_json_item	*kore_json_find(struct kore_json_item *,
diff --git a/src/json.c b/src/json.c
@@ -292,6 +292,24 @@ kore_json_item_tobuf(struct kore_json_item *item, struct kore_buf *buf)
 	}
 }
 
+void
+kore_json_item_attach(struct kore_json_item *parent,
+    struct kore_json_item *item)
+{
+	if (item->parent != NULL)
+		fatal("%s: item already has parent", __func__);
+
+	item->parent = parent;
+
+	if (parent->type != KORE_JSON_TYPE_OBJECT &&
+	    parent->type != KORE_JSON_TYPE_ARRAY) {
+		fatal("%s: invalid parent type (%d)",
+		    __func__, parent->type);
+	}
+
+	TAILQ_INSERT_TAIL(&parent->data.items, item, list);
+}
+
 static struct kore_json_item *
 json_find_item(struct kore_json_item *object, char **tokens,
     u_int32_t type, int pos)