From nobody Sat Nov 23 16:46:32 2024 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 1723671980599613.9062804674428; Wed, 14 Aug 2024 14:46:20 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 9B66E12C3; Wed, 14 Aug 2024 17:46:19 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 8670F1522; Wed, 14 Aug 2024 17:43:21 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 0ABC512B4; Wed, 14 Aug 2024 17:43:15 -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 9E0551282 for ; Wed, 14 Aug 2024 17:43:11 -0400 (EDT) Received: from mx-prod-mc-05.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-533-vDXEZyjdM7WMQNCYHnUEzQ-1; Wed, 14 Aug 2024 17:43:10 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 425D01955F03 for ; Wed, 14 Aug 2024 21:43:09 +0000 (UTC) Received: from hjs.brq.redhat.com (unknown [10.43.3.187]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 76EA91955E8C for ; Wed, 14 Aug 2024 21:43:08 +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=-0.6 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,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723671791; 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=ar8PjKcVZJZG3ZcnQclx8/rC9xXjnPGpqC3ZM1uOxKg=; b=CRYbbsAzCf6gP6Q2oj8woPj+KLar5I76pb82bFXcMmZ358D/xGONMa1hoaJ1QYPHWLCUem BqfHA2/r7iD6qI6QG0aU+KMNT0+7Ph5tvAhzPDHl3U2xUgL+SWizAQjihzSpFebBt3vNZT suBtag9ddgqbeRBlP7lsfD/pfffnzfA= X-MC-Unique: vDXEZyjdM7WMQNCYHnUEzQ-1 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: devel@lists.libvirt.org Subject: [libvirt PATCH 03/20] util: json: introduce virJSONStringPrettifyBlanks Date: Wed, 14 Aug 2024 23:40:18 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: WMZ72E7JOPKZ2VTJTC4O4PMPR5X35CH7 X-Message-ID-Hash: WMZ72E7JOPKZ2VTJTC4O4PMPR5X35CH7 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: 1723671982508116600 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 0accca442a..01b332a905 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2609,6 +2609,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.45.2