]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: json: Add helper to reformat JSON strings
authorPeter Krempa <pkrempa@redhat.com>
Wed, 30 Nov 2016 09:46:57 +0000 (10:46 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 18 Jan 2017 08:57:06 +0000 (09:57 +0100)
For use in test cases it will be helpful to allow reformatting JSON
strings. Add a wrapper on top of the parser and formatter to achieve
this.

src/libvirt_private.syms
src/util/virjson.c
src/util/virjson.h

index 70ed87b9537b3b071452b54a12ca82e55b43ceb3..cfeb43cf0d6aebc09a3510b31659cef978b38042 100644 (file)
@@ -1791,6 +1791,7 @@ virISCSIScanTargets;
 
 
 # util/virjson.h
+virJSONStringReformat;
 virJSONValueArrayAppend;
 virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
index 3804e7e9776317db0eb05273163126d5304b91b6..c907b5ded1c11d7ba77d7b72dd4de65ceebe47e5 100644 (file)
@@ -1918,3 +1918,32 @@ virJSONValueToString(virJSONValuePtr object ATTRIBUTE_UNUSED,
     return NULL;
 }
 #endif
+
+
+/**
+ * virJSONStringReformat:
+ * @jsonstr: string to reformat
+ * @pretty: use the pretty formatter
+ *
+ * Reformats a JSON string by passing it to the parser and then to the
+ * formatter. If @pretty is true the JSON is formatted for human eye
+ * compatibility.
+ *
+ * Returns the reformatted JSON string on success; NULL and a libvirt error on
+ * failure.
+ */
+char *
+virJSONStringReformat(const char *jsonstr,
+                      bool pretty)
+{
+    virJSONValuePtr json;
+    char *ret;
+
+    if (!(json = virJSONValueFromString(jsonstr)))
+        return NULL;
+
+    ret = virJSONValueToString(json, pretty);
+
+    virJSONValueFree(json);
+    return ret;
+}
index 122de96c522def6d8ff13d99793ad96b94ef3520..5e32cb9a411f03c4891de3299950b74afc6f2496 100644 (file)
@@ -181,4 +181,6 @@ int virJSONValueObjectForeachKeyValue(virJSONValuePtr object,
 
 virJSONValuePtr virJSONValueCopy(const virJSONValue *in);
 
+char *virJSONStringReformat(const char *jsonstr, bool pretty);
+
 #endif /* __VIR_JSON_H_ */