From patchwork Fri Dec 26 14:42:45 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 424126 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 0EBF5140082 for ; Sat, 27 Dec 2014 01:43:34 +1100 (AEDT) Received: from localhost ([::1]:53623 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y4W72-00023E-25 for incoming@patchwork.ozlabs.org; Fri, 26 Dec 2014 09:43:32 -0500 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 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id BE4A0ACD8; Fri, 26 Dec 2014 14:42:48 +0000 (UTC) From: Alexander Graf To: qemu-devel@nongnu.org Date: Fri, 26 Dec 2014 15:42:45 +0100 Message-Id: <1419604968-87437-3-git-send-email-agraf@suse.de> X-Mailer: git-send-email 1.7.12.4 In-Reply-To: <1419604968-87437-1-git-send-email-agraf@suse.de> References: <1419604968-87437-1-git-send-email-agraf@suse.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] X-Received-From: 195.135.220.15 Cc: amit.shah@redhat.com, pbonzini@redhat.com, afaerber@suse.de, quintela@redhat.com Subject: [Qemu-devel] [PATCH v3 2/5] QJSON: Add JSON writer X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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)