Received: from eggs.gnu.org ([2001:4830:134:3::10]:38808) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y4W6V-00016q-Ft for qemu-devel@nongnu.org; Fri, 26 Dec 2014 09:43:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y4W6N-0000PR-Ed for qemu-devel@nongnu.org; Fri, 26 Dec 2014 09:42:59 -0500 Received: from cantor2.suse.de ([195.135.220.15]:59153 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y4W6N-0000PM-7m for qemu-devel@nongnu.org; Fri, 26 Dec 2014 09:42:51 -0500 From: Alexander Graf Date: Fri, 26 Dec 2014 15:42:45 +0100 Message-Id: <1419604968-87437-3-git-send-email-agraf@suse.de> In-Reply-To: <1419604968-87437-1-git-send-email-agraf@suse.de> References: <1419604968-87437-1-git-send-email-agraf@suse.de> Subject: [Qemu-devel] [PATCH v3 2/5] QJSON: Add JSON writer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: amit.shah@redhat.com, pbonzini@redhat.com, afaerber@suse.de, quintela@redhat.com 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 --- 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 * * 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 #include #include +#include +#include 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