[PATCH 02/11] util: json: Add helper to reformat JSON strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 |  1 +
 src/util/virjson.c       | 29 +++++++++++++++++++++++++++++
 src/util/virjson.h       |  2 ++
 3 files changed, 32 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 867acdb..bd80f62 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1769,6 +1769,7 @@ virISCSIScanTargets;


 # util/virjson.h
+virJSONStringReformat;
 virJSONValueArrayAppend;
 virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index b42211c..d96f62a 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -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;
+}
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 5b4f172..3340a79 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -181,4 +181,6 @@ int virJSONValueObjectForeachKeyValue(const virJSONValue *object,

 virJSONValuePtr virJSONValueCopy(const virJSONValue *in);

+char *virJSONStringReformat(const char *jsonstr, bool pretty);
+
 #endif /* __VIR_JSON_H_ */
-- 
2.10.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux