[PATCH 02/23] Allow control over JSON string pretty printing

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

While the QEMU monitor/agent do not want JSON strings pretty
printed, other parts of libvirt might. Instead of hardcoding
QEMU's desired behaviour in virJSONValueToString(), add a
boolean flag to control pretty printing

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 src/qemu/qemu_agent.c        | 10 +++++-----
 src/qemu/qemu_monitor_json.c | 12 ++++++------
 src/util/json.c              |  9 +++++----
 src/util/json.h              |  3 ++-
 4 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 15af758..513f1d5 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -993,7 +993,7 @@ qemuAgentCommand(qemuAgentPtr mon,
 
     memset(&msg, 0, sizeof(msg));
 
-    if (!(cmdstr = virJSONValueToString(cmd))) {
+    if (!(cmdstr = virJSONValueToString(cmd, false))) {
         virReportOOMError();
         goto cleanup;
     }
@@ -1107,8 +1107,8 @@ qemuAgentCheckError(virJSONValuePtr cmd,
 {
     if (virJSONValueObjectHasKey(reply, "error")) {
         virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
-        char *cmdstr = virJSONValueToString(cmd);
-        char *replystr = virJSONValueToString(reply);
+        char *cmdstr = virJSONValueToString(cmd, false);
+        char *replystr = virJSONValueToString(reply, false);
 
         /* Log the full JSON formatted command & error */
         VIR_DEBUG("unable to execute QEMU command %s: %s",
@@ -1129,8 +1129,8 @@ qemuAgentCheckError(virJSONValuePtr cmd,
         VIR_FREE(replystr);
         return -1;
     } else if (!virJSONValueObjectHasKey(reply, "return")) {
-        char *cmdstr = virJSONValueToString(cmd);
-        char *replystr = virJSONValueToString(reply);
+        char *cmdstr = virJSONValueToString(cmd, false);
+        char *replystr = virJSONValueToString(reply, false);
 
         VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
                   cmdstr, replystr);
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 3ede88d..14842e9 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -243,7 +243,7 @@ qemuMonitorJSONCommandWithFd(qemuMonitorPtr mon,
         }
     }
 
-    if (!(cmdstr = virJSONValueToString(cmd))) {
+    if (!(cmdstr = virJSONValueToString(cmd, false))) {
         virReportOOMError();
         goto cleanup;
     }
@@ -328,8 +328,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd,
 {
     if (virJSONValueObjectHasKey(reply, "error")) {
         virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
-        char *cmdstr = virJSONValueToString(cmd);
-        char *replystr = virJSONValueToString(reply);
+        char *cmdstr = virJSONValueToString(cmd, false);
+        char *replystr = virJSONValueToString(reply, false);
 
         /* Log the full JSON formatted command & error */
         VIR_DEBUG("unable to execute QEMU command %s: %s",
@@ -350,8 +350,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd,
         VIR_FREE(replystr);
         return -1;
     } else if (!virJSONValueObjectHasKey(reply, "return")) {
-        char *cmdstr = virJSONValueToString(cmd);
-        char *replystr = virJSONValueToString(reply);
+        char *cmdstr = virJSONValueToString(cmd, false);
+        char *replystr = virJSONValueToString(reply, false);
 
         VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
                   cmdstr, replystr);
@@ -3354,7 +3354,7 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
         if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
             goto cleanup;
 
-        if (!(*reply_str = virJSONValueToString(reply)))
+        if (!(*reply_str = virJSONValueToString(reply, false)))
             goto cleanup;
     }
 
diff --git a/src/util/json.c b/src/util/json.c
index 5132989..8687a16 100644
--- a/src/util/json.c
+++ b/src/util/json.c
@@ -1054,14 +1054,15 @@ static int virJSONValueToStringOne(virJSONValuePtr object,
     return 0;
 }
 
-char *virJSONValueToString(virJSONValuePtr object)
+char *virJSONValueToString(virJSONValuePtr object,
+                           bool pretty)
 {
     yajl_gen g;
     const unsigned char *str;
     char *ret = NULL;
     yajl_size_t len;
 # ifndef HAVE_YAJL2
-    yajl_gen_config conf = { 0, " " }; /* Turns off pretty printing since QEMU can't cope */
+    yajl_gen_config conf = { pretty ? 1 : 0, pretty ? "    " : " "};
 # endif
 
     VIR_DEBUG("object=%p", object);
@@ -1069,8 +1070,8 @@ char *virJSONValueToString(virJSONValuePtr object)
 # ifdef HAVE_YAJL2
     g = yajl_gen_alloc(NULL);
     if (g) {
-        yajl_gen_config(g, yajl_gen_beautify, 0);
-        yajl_gen_config(g, yajl_gen_indent_string, " ");
+        yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0);
+        yajl_gen_config(g, yajl_gen_indent_string, pretty ? "    " : " ");
         yajl_gen_config(g, yajl_gen_validate_utf8, 1);
     }
 # else
diff --git a/src/util/json.h b/src/util/json.h
index c8bd504..bdba3dd 100644
--- a/src/util/json.h
+++ b/src/util/json.h
@@ -132,6 +132,7 @@ int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int
 int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key);
 
 virJSONValuePtr virJSONValueFromString(const char *jsonstring);
-char *virJSONValueToString(virJSONValuePtr object);
+char *virJSONValueToString(virJSONValuePtr object,
+                           bool pretty);
 
 #endif /* __VIR_JSON_H_ */
-- 
1.7.11.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]