From nobody Sun Feb 8 18:33:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1553866570838454.5939714366932; Fri, 29 Mar 2019 06:36:10 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 72F5D88AB2; Fri, 29 Mar 2019 13:36:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4E8B91001DD6; Fri, 29 Mar 2019 13:36:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0B80E18033A2; Fri, 29 Mar 2019 13:36:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2TDXngK026960 for ; Fri, 29 Mar 2019 09:33:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id F17D817C69; Fri, 29 Mar 2019 13:33:49 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7AC4E18E3F for ; Fri, 29 Mar 2019 13:33:49 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Fri, 29 Mar 2019 14:33:40 +0100 Message-Id: <3daf3f8f54ffd9dd899ed15d99086aabfa8dda2e.1553866155.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/7] util: json: Use virBuffer in JSON->string conversion X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 29 Mar 2019 13:36:10 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The last step of the conversion involves copying of the generated JSON into a separate string. We can use a virBuffer to do this as this will also allow to subsequently use the buffer when we actually need to do some other formatting of the string. Signed-off-by: Peter Krempa Reviewed-by: Laine Stump --- src/util/virjson.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index d5d66f879f..7dfc589944 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -28,6 +28,7 @@ #include "virlog.h" #include "virstring.h" #include "virutil.h" +#include "virbuffer.h" #if WITH_YAJL # include @@ -1969,17 +1970,18 @@ virJSONValueToStringOne(virJSONValuePtr object, } -char * -virJSONValueToString(virJSONValuePtr object, +static int +virJSONValueToBuffer(virJSONValuePtr object, + virBufferPtr buf, bool pretty) { yajl_gen g; const unsigned char *str; - char *ret =3D NULL; yajl_size_t len; # ifndef WITH_YAJL2 yajl_gen_config conf =3D { pretty ? 1 : 0, pretty ? " " : " "}; # endif + int ret =3D -1; VIR_DEBUG("object=3D%p", object); @@ -2009,13 +2011,12 @@ virJSONValueToString(virJSONValuePtr object, goto cleanup; } - ignore_value(VIR_STRDUP(ret, (const char *)str)); + virBufferAdd(buf, (const char *) str, len); + ret =3D 0; cleanup: yajl_gen_free(g); - VIR_DEBUG("result=3D%s", NULLSTR(ret)); - return ret; } @@ -2030,17 +2031,36 @@ virJSONValueFromString(const char *jsonstring ATTRI= BUTE_UNUSED) } -char * -virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED, +static int +virJSONValueToBuffer(virJSONValuePtr object ATTRIBUTE_UNUSED, + virBufferPtr buf ATTRIBUTE_UNUSED, bool pretty ATTRIBUTE_UNUSED) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("No JSON parser implementation is available")); - return NULL; + return -1; } #endif +char * +virJSONValueToString(virJSONValuePtr object, + bool pretty) +{ + VIR_AUTOCLEAN(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + char *ret =3D NULL; + + if (virJSONValueToBuffer(object, &buf, pretty) < 0) + return NULL; + + ret =3D virBufferContentAndReset(&buf); + + VIR_DEBUG("result=3D%s", NULLSTR(ret)); + + return ret; +} + + /** * virJSONStringReformat: * @jsonstr: string to reformat --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list