qjson.c 7.0 KB
Newer Older
A
Anthony Liguori 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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.
 *
 */

14 15 16 17 18 19 20 21 22
#include "qapi/qmp/json-lexer.h"
#include "qapi/qmp/json-parser.h"
#include "qapi/qmp/json-streamer.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qmp/qint.h"
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qbool.h"
#include "qapi/qmp/qfloat.h"
#include "qapi/qmp/qdict.h"
A
Anthony Liguori 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36

typedef struct JSONParsingState
{
    JSONMessageParser parser;
    va_list *ap;
    QObject *result;
} JSONParsingState;

static void parse_json(JSONMessageParser *parser, QList *tokens)
{
    JSONParsingState *s = container_of(parser, JSONParsingState, parser);
    s->result = json_parser_parse(tokens, s->ap);
}

37
QObject *qobject_from_jsonv(const char *string, va_list *ap)
A
Anthony Liguori 已提交
38 39 40
{
    JSONParsingState state = {};

41 42
    state.ap = ap;

A
Anthony Liguori 已提交
43 44 45 46 47 48 49 50
    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);

    return state.result;
}

51 52 53 54 55
QObject *qobject_from_json(const char *string)
{
    return qobject_from_jsonv(string, NULL);
}

L
Luiz Capitulino 已提交
56 57 58 59
/*
 * IMPORTANT: This function aborts on error, thus it must not
 * be used with untrusted arguments.
 */
A
Anthony Liguori 已提交
60 61
QObject *qobject_from_jsonf(const char *string, ...)
{
62
    QObject *obj;
A
Anthony Liguori 已提交
63 64 65
    va_list ap;

    va_start(ap, string);
66
    obj = qobject_from_jsonv(string, &ap);
A
Anthony Liguori 已提交
67 68
    va_end(ap);

L
Luiz Capitulino 已提交
69
    assert(obj != NULL);
70
    return obj;
A
Anthony Liguori 已提交
71
}
72 73 74

typedef struct ToJsonIterState
{
75 76
    int indent;
    int pretty;
77 78 79 80
    int count;
    QString *str;
} ToJsonIterState;

81
static void to_json(const QObject *obj, QString *str, int pretty, int indent);
82 83 84 85 86

static void to_json_dict_iter(const char *key, QObject *obj, void *opaque)
{
    ToJsonIterState *s = opaque;
    QString *qkey;
87
    int j;
88

89 90 91
    if (s->count) {
        qstring_append(s->str, s->pretty ? "," : ", ");
    }
92 93 94 95 96

    if (s->pretty) {
        qstring_append(s->str, "\n");
        for (j = 0 ; j < s->indent ; j++)
            qstring_append(s->str, "    ");
97 98 99
    }

    qkey = qstring_from_str(key);
100
    to_json(QOBJECT(qkey), s->str, s->pretty, s->indent);
101 102 103
    QDECREF(qkey);

    qstring_append(s->str, ": ");
104
    to_json(obj, s->str, s->pretty, s->indent);
105 106 107 108 109 110
    s->count++;
}

static void to_json_list_iter(QObject *obj, void *opaque)
{
    ToJsonIterState *s = opaque;
111
    int j;
112

113 114 115
    if (s->count) {
        qstring_append(s->str, s->pretty ? "," : ", ");
    }
116 117 118 119 120

    if (s->pretty) {
        qstring_append(s->str, "\n");
        for (j = 0 ; j < s->indent ; j++)
            qstring_append(s->str, "    ");
121 122
    }

123
    to_json(obj, s->str, s->pretty, s->indent);
124 125 126
    s->count++;
}

127
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
128 129
{
    switch (qobject_type(obj)) {
130 131 132
    case QTYPE_QNULL:
        qstring_append(str, "null");
        break;
133 134 135 136 137 138 139 140 141 142 143
    case QTYPE_QINT: {
        QInt *val = qobject_to_qint(obj);
        char buffer[1024];

        snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val));
        qstring_append(str, buffer);
        break;
    }
    case QTYPE_QSTRING: {
        QString *val = qobject_to_qstring(obj);
        const char *ptr;
144 145 146
        int cp;
        char buf[16];
        char *end;
147 148 149

        ptr = qstring_get_str(val);
        qstring_append(str, "\"");
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

        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 */
178
                }
179 180 181 182 183 184 185 186 187 188
                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;
189
                }
190 191 192 193
                qstring_append(str, buf);
            }
        };

194 195 196 197 198 199 200 201 202
        qstring_append(str, "\"");
        break;
    }
    case QTYPE_QDICT: {
        ToJsonIterState s;
        QDict *val = qobject_to_qdict(obj);

        s.count = 0;
        s.str = str;
203 204
        s.indent = indent + 1;
        s.pretty = pretty;
205 206
        qstring_append(str, "{");
        qdict_iter(val, to_json_dict_iter, &s);
207 208 209 210 211 212
        if (pretty) {
            int j;
            qstring_append(str, "\n");
            for (j = 0 ; j < indent ; j++)
                qstring_append(str, "    ");
        }
213 214 215 216 217 218 219 220 221
        qstring_append(str, "}");
        break;
    }
    case QTYPE_QLIST: {
        ToJsonIterState s;
        QList *val = qobject_to_qlist(obj);

        s.count = 0;
        s.str = str;
222 223
        s.indent = indent + 1;
        s.pretty = pretty;
224 225
        qstring_append(str, "[");
        qlist_iter(val, (void *)to_json_list_iter, &s);
226 227 228 229 230 231
        if (pretty) {
            int j;
            qstring_append(str, "\n");
            for (j = 0 ; j < indent ; j++)
                qstring_append(str, "    ");
        }
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        qstring_append(str, "]");
        break;
    }
    case QTYPE_QFLOAT: {
        QFloat *val = qobject_to_qfloat(obj);
        char buffer[1024];
        int len;

        len = snprintf(buffer, sizeof(buffer), "%f", qfloat_get_double(val));
        while (len > 0 && buffer[len - 1] == '0') {
            len--;
        }

        if (len && buffer[len - 1] == '.') {
            buffer[len - 1] = 0;
        } else {
            buffer[len] = 0;
        }
        
        qstring_append(str, buffer);
        break;
    }
    case QTYPE_QBOOL: {
        QBool *val = qobject_to_qbool(obj);

        if (qbool_get_int(val)) {
            qstring_append(str, "true");
        } else {
            qstring_append(str, "false");
        }
        break;
    }
L
Luiz Capitulino 已提交
264 265
    case QTYPE_QERROR:
        /* XXX: should QError be emitted? */
266
        break;
267
    default:
K
Kevin Wolf 已提交
268
        abort();
269 270 271 272 273 274 275
    }
}

QString *qobject_to_json(const QObject *obj)
{
    QString *str = qstring_new();

276 277 278 279 280 281 282 283 284 285
    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);
286 287 288

    return str;
}