2009-11-11 19:42:41 +03:00
|
|
|
/*
|
|
|
|
* QObject JSON integration
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2009
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:01 +03:00
|
|
|
#include "qemu/osdep.h"
|
2017-03-01 00:26:54 +03:00
|
|
|
#include "qapi/error.h"
|
2012-12-17 21:19:43 +04:00
|
|
|
#include "qapi/qmp/json-lexer.h"
|
|
|
|
#include "qapi/qmp/json-parser.h"
|
|
|
|
#include "qapi/qmp/json-streamer.h"
|
|
|
|
#include "qapi/qmp/qjson.h"
|
2018-02-01 14:18:35 +03:00
|
|
|
#include "qapi/qmp/qbool.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2018-02-01 14:18:38 +03:00
|
|
|
#include "qapi/qmp/qlist.h"
|
2018-02-01 14:18:36 +03:00
|
|
|
#include "qapi/qmp/qnum.h"
|
2018-02-01 14:18:40 +03:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/unicode.h"
|
2009-11-11 19:42:41 +03:00
|
|
|
|
|
|
|
typedef struct JSONParsingState
|
|
|
|
{
|
|
|
|
JSONMessageParser parser;
|
|
|
|
va_list *ap;
|
|
|
|
QObject *result;
|
2017-03-01 00:26:54 +03:00
|
|
|
Error *err;
|
2009-11-11 19:42:41 +03:00
|
|
|
} JSONParsingState;
|
|
|
|
|
2015-11-26 00:23:31 +03:00
|
|
|
static void parse_json(JSONMessageParser *parser, GQueue *tokens)
|
2009-11-11 19:42:41 +03:00
|
|
|
{
|
|
|
|
JSONParsingState *s = container_of(parser, JSONParsingState, parser);
|
2017-03-01 00:26:54 +03:00
|
|
|
|
|
|
|
s->result = json_parser_parse_err(tokens, s->ap, &s->err);
|
2009-11-11 19:42:41 +03:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:26:54 +03:00
|
|
|
QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
|
2009-11-11 19:42:41 +03:00
|
|
|
{
|
|
|
|
JSONParsingState state = {};
|
|
|
|
|
2009-11-19 04:05:24 +03:00
|
|
|
state.ap = ap;
|
|
|
|
|
2009-11-11 19:42:41 +03:00
|
|
|
json_message_parser_init(&state.parser, parse_json);
|
|
|
|
json_message_parser_feed(&state.parser, string, strlen(string));
|
|
|
|
json_message_parser_flush(&state.parser);
|
|
|
|
json_message_parser_destroy(&state.parser);
|
|
|
|
|
2017-03-01 00:26:54 +03:00
|
|
|
error_propagate(errp, state.err);
|
2009-11-11 19:42:41 +03:00
|
|
|
return state.result;
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:26:58 +03:00
|
|
|
QObject *qobject_from_json(const char *string, Error **errp)
|
2009-11-19 04:05:24 +03:00
|
|
|
{
|
2017-03-01 00:26:58 +03:00
|
|
|
return qobject_from_jsonv(string, NULL, errp);
|
2009-11-19 04:05:24 +03:00
|
|
|
}
|
|
|
|
|
2009-11-11 19:42:41 +03:00
|
|
|
QObject *qobject_from_jsonf(const char *string, ...)
|
|
|
|
{
|
2009-11-19 04:05:24 +03:00
|
|
|
QObject *obj;
|
2009-11-11 19:42:41 +03:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, string);
|
2017-03-01 00:26:56 +03:00
|
|
|
obj = qobject_from_jsonv(string, &ap, &error_abort);
|
2009-11-11 19:42:41 +03:00
|
|
|
va_end(ap);
|
|
|
|
|
2009-11-19 04:05:24 +03:00
|
|
|
return obj;
|
2009-11-11 19:42:41 +03:00
|
|
|
}
|
2009-11-11 21:01:22 +03:00
|
|
|
|
2018-07-03 11:53:47 +03:00
|
|
|
/*
|
|
|
|
* Parse @string as JSON object with %-escapes interpolated.
|
|
|
|
* Abort on error. Do not use with untrusted @string.
|
|
|
|
* Return the resulting QDict. It is never null.
|
|
|
|
*/
|
|
|
|
QDict *qdict_from_jsonf_nofail(const char *string, ...)
|
|
|
|
{
|
|
|
|
QDict *obj;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, string);
|
|
|
|
obj = qobject_to(QDict, qobject_from_jsonv(string, &ap, &error_abort));
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
assert(obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2009-11-11 21:01:22 +03:00
|
|
|
typedef struct ToJsonIterState
|
|
|
|
{
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
int indent;
|
|
|
|
int pretty;
|
2009-11-11 21:01:22 +03:00
|
|
|
int count;
|
|
|
|
QString *str;
|
|
|
|
} ToJsonIterState;
|
|
|
|
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
static void to_json(const QObject *obj, QString *str, int pretty, int indent);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
|
|
|
|
{
|
|
|
|
ToJsonIterState *s = opaque;
|
|
|
|
QString *qkey;
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
int j;
|
2009-11-11 21:01:22 +03:00
|
|
|
|
2014-11-21 12:29:59 +03:00
|
|
|
if (s->count) {
|
|
|
|
qstring_append(s->str, s->pretty ? "," : ", ");
|
|
|
|
}
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
|
|
|
|
if (s->pretty) {
|
|
|
|
qstring_append(s->str, "\n");
|
|
|
|
for (j = 0 ; j < s->indent ; j++)
|
|
|
|
qstring_append(s->str, " ");
|
2009-11-11 21:01:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qkey = qstring_from_str(key);
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(qkey);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
qstring_append(s->str, ": ");
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
to_json(obj, s->str, s->pretty, s->indent);
|
2009-11-11 21:01:22 +03:00
|
|
|
s->count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void to_json_list_iter(QObject *obj, void *opaque)
|
|
|
|
{
|
|
|
|
ToJsonIterState *s = opaque;
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
int j;
|
2009-11-11 21:01:22 +03:00
|
|
|
|
2014-11-21 12:29:59 +03:00
|
|
|
if (s->count) {
|
|
|
|
qstring_append(s->str, s->pretty ? "," : ", ");
|
|
|
|
}
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
|
|
|
|
if (s->pretty) {
|
|
|
|
qstring_append(s->str, "\n");
|
|
|
|
for (j = 0 ; j < s->indent ; j++)
|
|
|
|
qstring_append(s->str, " ");
|
2009-11-11 21:01:22 +03:00
|
|
|
}
|
|
|
|
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
to_json(obj, s->str, s->pretty, s->indent);
|
2009-11-11 21:01:22 +03:00
|
|
|
s->count++;
|
|
|
|
}
|
|
|
|
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
|
2009-11-11 21:01:22 +03:00
|
|
|
{
|
|
|
|
switch (qobject_type(obj)) {
|
2015-04-30 00:35:05 +03:00
|
|
|
case QTYPE_QNULL:
|
|
|
|
qstring_append(str, "null");
|
|
|
|
break;
|
2017-06-07 19:35:58 +03:00
|
|
|
case QTYPE_QNUM: {
|
2018-02-24 18:40:29 +03:00
|
|
|
QNum *val = qobject_to(QNum, obj);
|
2017-06-07 19:35:58 +03:00
|
|
|
char *buffer = qnum_to_string(val);
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, buffer);
|
2017-06-07 19:35:58 +03:00
|
|
|
g_free(buffer);
|
2009-11-11 21:01:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QTYPE_QSTRING: {
|
2018-02-24 18:40:29 +03:00
|
|
|
QString *val = qobject_to(QString, obj);
|
2009-11-11 21:01:22 +03:00
|
|
|
const char *ptr;
|
2013-04-11 20:07:21 +04:00
|
|
|
int cp;
|
|
|
|
char buf[16];
|
|
|
|
char *end;
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
ptr = qstring_get_str(val);
|
|
|
|
qstring_append(str, "\"");
|
2013-04-11 20:07:21 +04:00
|
|
|
|
|
|
|
for (; *ptr; ptr = end) {
|
|
|
|
cp = mod_utf8_codepoint(ptr, 6, &end);
|
|
|
|
switch (cp) {
|
|
|
|
case '\"':
|
|
|
|
qstring_append(str, "\\\"");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
qstring_append(str, "\\\\");
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
qstring_append(str, "\\b");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
qstring_append(str, "\\f");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
qstring_append(str, "\\n");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
qstring_append(str, "\\r");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
qstring_append(str, "\\t");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (cp < 0) {
|
|
|
|
cp = 0xFFFD; /* replacement character */
|
2009-11-11 21:01:22 +03:00
|
|
|
}
|
2013-04-11 20:07:21 +04:00
|
|
|
if (cp > 0xFFFF) {
|
|
|
|
/* beyond BMP; need a surrogate pair */
|
|
|
|
snprintf(buf, sizeof(buf), "\\u%04X\\u%04X",
|
|
|
|
0xD800 + ((cp - 0x10000) >> 10),
|
|
|
|
0xDC00 + ((cp - 0x10000) & 0x3FF));
|
|
|
|
} else if (cp < 0x20 || cp >= 0x7F) {
|
|
|
|
snprintf(buf, sizeof(buf), "\\u%04X", cp);
|
|
|
|
} else {
|
|
|
|
buf[0] = cp;
|
|
|
|
buf[1] = 0;
|
2009-11-11 21:01:22 +03:00
|
|
|
}
|
2013-04-11 20:07:21 +04:00
|
|
|
qstring_append(str, buf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "\"");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QTYPE_QDICT: {
|
|
|
|
ToJsonIterState s;
|
2018-02-24 18:40:29 +03:00
|
|
|
QDict *val = qobject_to(QDict, obj);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
s.count = 0;
|
|
|
|
s.str = str;
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
s.indent = indent + 1;
|
|
|
|
s.pretty = pretty;
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "{");
|
|
|
|
qdict_iter(val, to_json_dict_iter, &s);
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
if (pretty) {
|
|
|
|
int j;
|
|
|
|
qstring_append(str, "\n");
|
|
|
|
for (j = 0 ; j < indent ; j++)
|
|
|
|
qstring_append(str, " ");
|
|
|
|
}
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "}");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QTYPE_QLIST: {
|
|
|
|
ToJsonIterState s;
|
2018-02-24 18:40:29 +03:00
|
|
|
QList *val = qobject_to(QList, obj);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
s.count = 0;
|
|
|
|
s.str = str;
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
s.indent = indent + 1;
|
|
|
|
s.pretty = pretty;
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "[");
|
|
|
|
qlist_iter(val, (void *)to_json_list_iter, &s);
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
if (pretty) {
|
|
|
|
int j;
|
|
|
|
qstring_append(str, "\n");
|
|
|
|
for (j = 0 ; j < indent ; j++)
|
|
|
|
qstring_append(str, " ");
|
|
|
|
}
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "]");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QTYPE_QBOOL: {
|
2018-02-24 18:40:29 +03:00
|
|
|
QBool *val = qobject_to(QBool, obj);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
2015-05-16 01:24:59 +03:00
|
|
|
if (qbool_get_bool(val)) {
|
2009-11-11 21:01:22 +03:00
|
|
|
qstring_append(str, "true");
|
|
|
|
} else {
|
|
|
|
qstring_append(str, "false");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-04-30 00:35:04 +03:00
|
|
|
default:
|
2013-07-08 18:14:21 +04:00
|
|
|
abort();
|
2009-11-11 21:01:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString *qobject_to_json(const QObject *obj)
|
|
|
|
{
|
|
|
|
QString *str = qstring_new();
|
|
|
|
|
Add support for JSON pretty printing
The monitor does not pretty-print JSON output, so that everything
will be on a single line reply. When JSON docs get large this is
quite unpleasant to read. For the future command line capabilities
query ability, huge JSON docs will be available. This needs the
ability to pretty-print.
This introduces a new API qobject_to_json_pretty() that does
a minimal indentation of list and dict members. As an example,
this makes
{"QMP": {"version": {"micro": 50, "minor": 12, "package": "", "major": 0}, "capabilities": []}}
Output as
{
"QMP": {
"version": {
"micro": 50,
"minor": 12,
"package": "",
"major": 0
},
"capabilities": [
]
}
}
NB: this is not turned on for the QMP monitor.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2010-06-07 18:42:14 +04:00
|
|
|
to_json(obj, str, 0, 0);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString *qobject_to_json_pretty(const QObject *obj)
|
|
|
|
{
|
|
|
|
QString *str = qstring_new();
|
|
|
|
|
|
|
|
to_json(obj, str, 1, 0);
|
2009-11-11 21:01:22 +03:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|