From nobody Sun Feb 8 19:47:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1725545091282809.6478636828557; Thu, 5 Sep 2024 07:04:51 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 3B176A39; Thu, 5 Sep 2024 10:04:50 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 2D70D176A; Thu, 5 Sep 2024 09:55:02 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 370201689; Thu, 5 Sep 2024 09:54:55 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 16B28175C for ; Thu, 5 Sep 2024 09:50:30 -0400 (EDT) Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-639-YyHtHWstPi-qhkNOnsBheQ-1; Thu, 05 Sep 2024 09:50:28 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DBD211958B0D for ; Thu, 5 Sep 2024 13:50:24 +0000 (UTC) Received: from hjs.brq.redhat.com (unknown [10.43.3.187]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CC5F13000239 for ; Thu, 5 Sep 2024 13:50:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: *** X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725544229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H/qfAfDbltoiAuYte5yZaTvY/iGlcaJEK4ObDU9IyKk=; b=Z1uk+r95ATrOFY1+t4lNByE2tN4oOm/j4VMTRpQSrCQ1pl42GyIwA4cYC/2wGha0y/ZfxT 3pPlf7fmutzg6WYaQpjZDkyWSLi+EnJWkJznMpNrCfAE2X3k2ECEGAXp0jWRcQvTzdCi76 YH9bqBu90873OyQhbMsOxXG4t8HA8S0= X-MC-Unique: YyHtHWstPi-qhkNOnsBheQ-1 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: devel@lists.libvirt.org Subject: [libvirt PATCHv2 01/15] util: json: introduce virJSONStringPrettifyBlanks Date: Thu, 5 Sep 2024 15:49:28 +0200 Message-ID: <27ebcbc51e6976f3021f3e91d1df58eb491ba8f5.1725544182.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: PUPNPRDFBCJX65EWLJEFGDTCAPJRJFSL X-Message-ID-Hash: PUPNPRDFBCJX65EWLJEFGDTCAPJRJFSL X-MailFrom: jtomko@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1725545092379116600 Content-Type: text/plain; charset="utf-8" A horribly named function for unifying formatting when pretty-printing empty JSON arrays and objects. Useful for having stable test output even if different JSON libraries format these differently. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virjson.c | 34 ++++++++++++++++++++++++++++++++++ src/util/virjson.h | 2 ++ 3 files changed, 37 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index af40e5dca3..d186dc40df 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2612,6 +2612,7 @@ virISCSIScanTargets; =20 =20 # util/virjson.h +virJSONStringPrettifyBlanks; virJSONStringReformat; virJSONValueArrayAppend; virJSONValueArrayAppendString; diff --git a/src/util/virjson.c b/src/util/virjson.c index 57707350da..0edf86cd1c 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1861,6 +1861,40 @@ virJSONStringReformat(const char *jsonstr, return virJSONValueToString(json, pretty); } =20 +/** + * virJSONStringPrettifyBlanks: + * @jsonstr: string to prettify + * + * In the pretty mode of printing, various versions of JSON libraries + * format empty arrays and objects differently. + * + * Unify this to "[]" and "{}" which are used by json-c 0.17 and newer. + * https://github.com/json-c/json-c/issues/778 + * + * This format is also used by Python's 'json.dump' method. + * + * Returns the reformatted JSON string on success. + */ +char *virJSONStringPrettifyBlanks(const char *jsonstr) +{ + virBuffer buf =3D VIR_BUFFER_INITIALIZER; + const char *p; + + for (p =3D jsonstr; *p && p[1]; p++) { + virBufferAddChar(&buf, *p); + + if ((p[0] =3D=3D '{' || p[0] =3D=3D '[') && p[1] =3D=3D '\n') { + const char *q =3D p + 1; + + virSkipSpaces(&q); + + if (*q =3D=3D '}' || *q =3D=3D ']') + p =3D q - 1; + } + } + + return virBufferContentAndReset(&buf); +} =20 static virJSONValue * virJSONValueObjectDeflattenKeys(virJSONValue *json); diff --git a/src/util/virjson.h b/src/util/virjson.h index e622798fe7..d8481e5890 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -271,6 +271,8 @@ virJSONValueCopy(const virJSONValue *in); char * virJSONStringReformat(const char *jsonstr, bool pretty); +char * +virJSONStringPrettifyBlanks(const char *jsonstr); =20 virJSONValue * virJSONValueObjectDeflatten(virJSONValue *json); --=20 2.46.0