[PATCH 05/12] Add a JSON generator API.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This allows incremental use of virJSONStringGeneratorAddProperties
without constructing a full object first, while isolating the rest of
libvirt from the yajl dependency.

Will be used later.

Signed-off-by: Miloslav Trmač <mitr@xxxxxxxxxx>
---
 src/util/json.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/util/json.h |  3 +++
 2 files changed, 63 insertions(+)

diff --git a/src/util/json.c b/src/util/json.c
index 9b0a6dd..03362c4 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -1094,6 +1094,66 @@ static yajl_gen virYAJLInit(bool pretty)
     return g;
 }
 
+/**
+ * virJSONStringGeneratorInitObject:
+ *
+ * Allocate a generator for creating a string representation of an object using
+ * one or more calls to virJSONStringGeneratorAddProperties().
+ *
+ * Returns the generator on success, NULL on error.
+ */
+virJSONStringGeneratorPtr virJSONStringGeneratorInitObject(void)
+{
+    yajl_gen g;
+
+    g = virYAJLInit(false);
+    if (g == NULL)
+        goto error;
+    if (yajl_gen_map_open(g) != yajl_gen_status_ok)
+        goto error;
+    return g;
+
+ error:
+    if (g != NULL)
+        yajl_gen_free(g);
+    return NULL;
+}
+
+/**
+ * virJSONStringGeneratorFinishObject:
+ * @gen: the destination generator
+ *
+ * Terminate the object being generated in @gen, and return its string value
+ * (or NULL on error).  The value is valid until @gen is freed.
+ */
+const char *virJSONStringGeneratorFinishObject(virJSONStringGeneratorPtr gen)
+{
+    const unsigned char *res;
+    yajl_size_t len;
+
+    if (yajl_gen_map_close(gen) != yajl_gen_status_ok)
+        goto error;
+    if (yajl_gen_get_buf(gen, &res, &len) != yajl_gen_status_ok)
+        goto error;
+    return (const char *)res;
+
+ error:
+    return NULL;
+}
+
+/**
+ * virJSONStringGeneratorFree:
+ * @gen: the generator
+ *
+ * Free @gen if it is not NULL.  Invalidates the result of
+ * virJSONStringGeneratorFinishObject().
+ */
+void virJSONStringGeneratorFree(virJSONStringGeneratorPtr gen)
+{
+    if (gen != NULL)
+        yajl_gen_free(gen);
+}
+
 static int virJSONValueToStringOne(virJSONValuePtr object,
                                    yajl_gen g)
 {
diff --git a/src/util/json.h b/src/util/json.h
index 390e68d..80041eb 100644
--- a/src/util/json.h
+++ b/src/util/json.h
@@ -151,8 +151,11 @@ void virJSONStaticObjectAppendNumberInt(virJSONObjectPtr object,
                                         char *buf, size_t buf_size,
                                         int value);
 
+virJSONStringGeneratorPtr virJSONStringGeneratorInitObject(void);
 int virJSONStringGeneratorAddProperties(virJSONStringGeneratorPtr gen,
                                         virJSONObjectPtr object);
+const char *virJSONStringGeneratorFinishObject(virJSONStringGeneratorPtr gen);
+void virJSONStringGeneratorFree(virJSONStringGeneratorPtr gen);
 
 virJSONValuePtr virJSONValueFromString(const char *jsonstring);
 char *virJSONValueToString(virJSONValuePtr object,
-- 
1.7.11.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]