[libvirt PATCH 03/20] util: json: introduce virJSONStringPrettifyBlanks

Ján Tomko posted 20 patches 3 months, 1 week ago
There is a newer version of this series
[libvirt PATCH 03/20] util: json: introduce virJSONStringPrettifyBlanks
Posted by Ján Tomko 3 months, 1 week ago
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án Tomko <jtomko@redhat.com>
---
 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;
 
 
 # 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);
 }
 
+/**
+ * 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 = VIR_BUFFER_INITIALIZER;
+    const char *p;
+
+    for (p = jsonstr; *p && p[1]; p++) {
+        virBufferAddChar(&buf, *p);
+
+        if ((p[0] == '{' || p[0] == '[') && p[1] == '\n') {
+            const char *q = p + 1;
+
+            virSkipSpaces(&q);
+
+            if (*q == '}' || *q == ']')
+                p = q - 1;
+        }
+    }
+
+    return virBufferContentAndReset(&buf);
+}
 
 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);
 
 virJSONValue *
 virJSONValueObjectDeflatten(virJSONValue *json);
-- 
2.45.2
Re: [libvirt PATCH 03/20] util: json: introduce virJSONStringPrettifyBlanks
Posted by Peter Krempa 3 months, 1 week ago
On Wed, Aug 14, 2024 at 23:40:18 +0200, Ján Tomko wrote:
> 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án Tomko <jtomko@redhat.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virjson.c       | 34 ++++++++++++++++++++++++++++++++++
>  src/util/virjson.h       |  2 ++
>  3 files changed, 37 insertions(+)

[...]

> 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);
>  }
>  
> +/**
> + * 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

Ah, I thought that we could drop this along with yajl removal ... too
bad.

Reviewed-by: Peter Krempa <pkrempa@redhat.com>