From patchwork Fri Apr 29 04:23:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 616583 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qx0w95J2tz9s5J for ; Fri, 29 Apr 2016 14:24:41 +1000 (AEST) Received: from localhost ([::1]:52200 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzyp-0000AW-BJ for incoming@patchwork.ozlabs.org; Fri, 29 Apr 2016 00:24:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37751) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy3-0007BI-9K for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avzy2-0000sn-Ca for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50338) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy2-0000sj-6W for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:50 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E34103B75A for ; Fri, 29 Apr 2016 04:23:49 +0000 (UTC) Received: from red.redhat.com (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3T4Ngf5016324; Fri, 29 Apr 2016 00:23:49 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 28 Apr 2016 22:23:29 -0600 Message-Id: <1461903820-3092-9-git-send-email-eblake@redhat.com> In-Reply-To: <1461903820-3092-1-git-send-email-eblake@redhat.com> References: <1461903820-3092-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 08/18] qjson: Simplify by using json-output-visitor X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: famz@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Instead of rolling our own limited JSON outputter, we can just wrap the more full-featured JSON output Visitor. This slightly changes the output (different spacing), but the result is still equivalent JSON contents. Signed-off-by: Eric Blake --- v3: rebase to master v2: rebase to earlier changes --- qjson.c | 61 ++++++++++++++++++++++--------------------------------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/qjson.c b/qjson.c index d172b1f..17cc962 100644 --- a/qjson.c +++ b/qjson.c @@ -12,102 +12,85 @@ */ #include "qemu/osdep.h" -#include -#include #include #include #include +#include "qapi/json-output-visitor.h" +#include "qapi/error.h" struct QJSON { Object obj; - QString *str; - bool omit_comma; + JsonOutputVisitor *jov; + char *str; }; #define QJSON(obj) OBJECT_CHECK(QJSON, (obj), TYPE_QJSON) -static void json_emit_element(QJSON *json, const char *name) -{ - /* Check whether we need to print a , before an element */ - if (json->omit_comma) { - json->omit_comma = false; - } else { - qstring_append(json->str, ", "); - } - - if (name) { - qstring_append_json_string(json->str, name); - qstring_append(json->str, " : "); - } -} - void json_start_object(QJSON *json, const char *name) { - json_emit_element(json, name); - qstring_append(json->str, "{ "); - json->omit_comma = true; + Visitor *v = json_output_get_visitor(json->jov); + visit_start_struct(v, name, NULL, 0, &error_abort); } void json_end_object(QJSON *json) { - qstring_append(json->str, " }"); - json->omit_comma = false; + Visitor *v = json_output_get_visitor(json->jov); + visit_check_struct(v, &error_abort); + visit_end_struct(v); } void json_start_array(QJSON *json, const char *name) { - json_emit_element(json, name); - qstring_append(json->str, "[ "); - json->omit_comma = true; + Visitor *v = json_output_get_visitor(json->jov); + visit_start_list(v, name, NULL, 0, &error_abort); } void json_end_array(QJSON *json) { - qstring_append(json->str, " ]"); - json->omit_comma = false; + Visitor *v = json_output_get_visitor(json->jov); + visit_end_list(v); } void json_prop_int(QJSON *json, const char *name, int64_t val) { - json_emit_element(json, name); - qstring_append_format(json->str, "%" PRId64, val); + Visitor *v = json_output_get_visitor(json->jov); + visit_type_int(v, name, &val, &error_abort); } void json_prop_str(QJSON *json, const char *name, const char *str) { - json_emit_element(json, name); - qstring_append_json_string(json->str, str); + Visitor *v = json_output_get_visitor(json->jov); + visit_type_str(v, name, (char **)&str, &error_abort); } const char *qjson_get_str(QJSON *json) { - return qstring_get_str(json->str); + return json->str; } QJSON *qjson_new(void) { QJSON *json = QJSON(object_new(TYPE_QJSON)); + json_start_object(json, NULL); return json; } void qjson_finish(QJSON *json) { json_end_object(json); + json->str = json_output_get_string(json->jov); } static void qjson_initfn(Object *obj) { QJSON *json = QJSON(obj); - - json->str = qstring_from_str("{ "); - json->omit_comma = true; + json->jov = json_output_visitor_new(); } static void qjson_finalizefn(Object *obj) { QJSON *json = QJSON(obj); - - qobject_decref(QOBJECT(json->str)); + g_free(json->str); } static const TypeInfo qjson_type_info = {