qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH v3 2/5] QJSON: Add JSON writer


From: Alexander Graf
Subject: [Qemu-devel] [PATCH v3 2/5] QJSON: Add JSON writer
Date: Fri, 26 Dec 2014 15:42:45 +0100

To support programmatic JSON assembly while keeping the code that generates it
readable, this patch introduces a simple JSON writer. It emits JSON serially
into a buffer in memory.

The nice thing about this writer is its simplicity and low memory overhead.
Unlike the QMP JSON writer, this one does not need to spawn QObjects for every
element it wants to represent.

This is a prerequisite for the migration stream format description generator.

Signed-off-by: Alexander Graf <address@hidden>

---

v2 -> v3:

  - QOMify the QJSON object, makes for easier destruction
---
 include/qjson.h |  3 ++-
 qjson.c         | 39 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/include/qjson.h b/include/qjson.h
index 8f8c145..7c54fdf 100644
--- a/include/qjson.h
+++ b/include/qjson.h
@@ -4,7 +4,7 @@
  * Copyright Alexander Graf
  *
  * Authors:
- *  Alexander Graf <address@hidden
+ *  Alexander Graf <address@hidden>
  *
  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  * See the COPYING.LIB file in the top-level directory.
@@ -13,6 +13,7 @@
 #ifndef QEMU_QJSON_H
 #define QEMU_QJSON_H
 
+#define TYPE_QJSON "QJSON"
 typedef struct QJSON QJSON;
 
 QJSON *qjson_new(void);
diff --git a/qjson.c b/qjson.c
index 7a7cd72..8d911d0 100644
--- a/qjson.c
+++ b/qjson.c
@@ -15,8 +15,11 @@
 #include <stdbool.h>
 #include <glib.h>
 #include <qjson.h>
+#include <qemu/module.h>
+#include <qom/object.h>
 
 struct QJSON {
+    Object obj;
     QString *str;
     bool omit_comma;
     unsigned long self_size_offset;
@@ -85,9 +88,7 @@ const char *qjson_get_str(QJSON *json)
 
 QJSON *qjson_new(void)
 {
-    QJSON *json = g_new(QJSON, 1);
-    json->str = qstring_from_str("{ ");
-    json->omit_comma = true;
+    QJSON *json = (QJSON *)object_new(TYPE_QJSON);
     return json;
 }
 
@@ -95,3 +96,35 @@ void qjson_finish(QJSON *json)
 {
     json_end_object(json);
 }
+
+static void qjson_initfn(Object *obj)
+{
+    QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
+    assert(json);
+
+    json->str = qstring_from_str("{ ");
+    json->omit_comma = true;
+}
+
+static void qjson_finalizefn(Object *obj)
+{
+    QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
+
+    assert(json);
+    qobject_decref(QOBJECT(json->str));
+}
+
+static const TypeInfo qjson_type_info = {
+    .name = TYPE_QJSON,
+    .parent = TYPE_OBJECT,
+    .instance_size = sizeof(QJSON),
+    .instance_init = qjson_initfn,
+    .instance_finalize = qjson_finalizefn,
+};
+
+static void qjson_register_types(void)
+{
+    type_register_static(&qjson_type_info);
+}
+
+type_init(qjson_register_types)
-- 
1.7.12.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]