提交 ecf00158 编写于 作者: D Daniel P. Berrange

Allow control over JSON string pretty printing

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: NDaniel P. Berrange <berrange@redhat.com>
上级 4ee86721
...@@ -993,7 +993,7 @@ qemuAgentCommand(qemuAgentPtr mon, ...@@ -993,7 +993,7 @@ qemuAgentCommand(qemuAgentPtr mon,
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
if (!(cmdstr = virJSONValueToString(cmd))) { if (!(cmdstr = virJSONValueToString(cmd, false))) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -1107,8 +1107,8 @@ qemuAgentCheckError(virJSONValuePtr cmd, ...@@ -1107,8 +1107,8 @@ qemuAgentCheckError(virJSONValuePtr cmd,
{ {
if (virJSONValueObjectHasKey(reply, "error")) { if (virJSONValueObjectHasKey(reply, "error")) {
virJSONValuePtr error = virJSONValueObjectGet(reply, "error"); virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
char *cmdstr = virJSONValueToString(cmd); char *cmdstr = virJSONValueToString(cmd, false);
char *replystr = virJSONValueToString(reply); char *replystr = virJSONValueToString(reply, false);
/* Log the full JSON formatted command & error */ /* Log the full JSON formatted command & error */
VIR_DEBUG("unable to execute QEMU command %s: %s", VIR_DEBUG("unable to execute QEMU command %s: %s",
...@@ -1129,8 +1129,8 @@ qemuAgentCheckError(virJSONValuePtr cmd, ...@@ -1129,8 +1129,8 @@ qemuAgentCheckError(virJSONValuePtr cmd,
VIR_FREE(replystr); VIR_FREE(replystr);
return -1; return -1;
} else if (!virJSONValueObjectHasKey(reply, "return")) { } else if (!virJSONValueObjectHasKey(reply, "return")) {
char *cmdstr = virJSONValueToString(cmd); char *cmdstr = virJSONValueToString(cmd, false);
char *replystr = virJSONValueToString(reply); char *replystr = virJSONValueToString(reply, false);
VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s", VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
cmdstr, replystr); cmdstr, replystr);
......
...@@ -243,7 +243,7 @@ qemuMonitorJSONCommandWithFd(qemuMonitorPtr mon, ...@@ -243,7 +243,7 @@ qemuMonitorJSONCommandWithFd(qemuMonitorPtr mon,
} }
} }
if (!(cmdstr = virJSONValueToString(cmd))) { if (!(cmdstr = virJSONValueToString(cmd, false))) {
virReportOOMError(); virReportOOMError();
goto cleanup; goto cleanup;
} }
...@@ -328,8 +328,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd, ...@@ -328,8 +328,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd,
{ {
if (virJSONValueObjectHasKey(reply, "error")) { if (virJSONValueObjectHasKey(reply, "error")) {
virJSONValuePtr error = virJSONValueObjectGet(reply, "error"); virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
char *cmdstr = virJSONValueToString(cmd); char *cmdstr = virJSONValueToString(cmd, false);
char *replystr = virJSONValueToString(reply); char *replystr = virJSONValueToString(reply, false);
/* Log the full JSON formatted command & error */ /* Log the full JSON formatted command & error */
VIR_DEBUG("unable to execute QEMU command %s: %s", VIR_DEBUG("unable to execute QEMU command %s: %s",
...@@ -350,8 +350,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd, ...@@ -350,8 +350,8 @@ qemuMonitorJSONCheckError(virJSONValuePtr cmd,
VIR_FREE(replystr); VIR_FREE(replystr);
return -1; return -1;
} else if (!virJSONValueObjectHasKey(reply, "return")) { } else if (!virJSONValueObjectHasKey(reply, "return")) {
char *cmdstr = virJSONValueToString(cmd); char *cmdstr = virJSONValueToString(cmd, false);
char *replystr = virJSONValueToString(reply); char *replystr = virJSONValueToString(reply, false);
VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s", VIR_DEBUG("Neither 'return' nor 'error' is set in the JSON reply %s: %s",
cmdstr, replystr); cmdstr, replystr);
...@@ -3354,7 +3354,7 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon, ...@@ -3354,7 +3354,7 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
goto cleanup; goto cleanup;
if (!(*reply_str = virJSONValueToString(reply))) if (!(*reply_str = virJSONValueToString(reply, false)))
goto cleanup; goto cleanup;
} }
......
...@@ -1054,14 +1054,15 @@ static int virJSONValueToStringOne(virJSONValuePtr object, ...@@ -1054,14 +1054,15 @@ static int virJSONValueToStringOne(virJSONValuePtr object,
return 0; return 0;
} }
char *virJSONValueToString(virJSONValuePtr object) char *virJSONValueToString(virJSONValuePtr object,
bool pretty)
{ {
yajl_gen g; yajl_gen g;
const unsigned char *str; const unsigned char *str;
char *ret = NULL; char *ret = NULL;
yajl_size_t len; yajl_size_t len;
# ifndef HAVE_YAJL2 # 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 # endif
VIR_DEBUG("object=%p", object); VIR_DEBUG("object=%p", object);
...@@ -1069,8 +1070,8 @@ char *virJSONValueToString(virJSONValuePtr object) ...@@ -1069,8 +1070,8 @@ char *virJSONValueToString(virJSONValuePtr object)
# ifdef HAVE_YAJL2 # ifdef HAVE_YAJL2
g = yajl_gen_alloc(NULL); g = yajl_gen_alloc(NULL);
if (g) { if (g) {
yajl_gen_config(g, yajl_gen_beautify, 0); yajl_gen_config(g, yajl_gen_beautify, pretty ? 1 : 0);
yajl_gen_config(g, yajl_gen_indent_string, " "); yajl_gen_config(g, yajl_gen_indent_string, pretty ? " " : " ");
yajl_gen_config(g, yajl_gen_validate_utf8, 1); yajl_gen_config(g, yajl_gen_validate_utf8, 1);
} }
# else # else
......
...@@ -132,6 +132,7 @@ int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int ...@@ -132,6 +132,7 @@ int virJSONValueObjectAppendBoolean(virJSONValuePtr object, const char *key, int
int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key); int virJSONValueObjectAppendNull(virJSONValuePtr object, const char *key);
virJSONValuePtr virJSONValueFromString(const char *jsonstring); virJSONValuePtr virJSONValueFromString(const char *jsonstring);
char *virJSONValueToString(virJSONValuePtr object); char *virJSONValueToString(virJSONValuePtr object,
bool pretty);
#endif /* __VIR_JSON_H_ */ #endif /* __VIR_JSON_H_ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册