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: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2012-08-09 11:46:29 +01:00
parent 4ee8672146
commit ecf00158ff
4 changed files with 18 additions and 16 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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_ */