qjson.c 6.8 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
    if (s->count)
90
        qstring_append(s->str, ", ");
91 92 93 94 95

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

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

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

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

112
    if (s->count)
113
        qstring_append(s->str, ", ");
114 115 116 117 118

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

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

125
static void to_json(const QObject *obj, QString *str, int pretty, int indent)
126 127 128 129 130 131 132 133 134 135 136 137 138
{
    switch (qobject_type(obj)) {
    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;
139 140 141
        int cp;
        char buf[16];
        char *end;
142 143 144

        ptr = qstring_get_str(val);
        qstring_append(str, "\"");
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        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 */
173
                }
174 175 176 177 178 179 180 181 182 183
                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;
184
                }
185 186 187 188
                qstring_append(str, buf);
            }
        };

189 190 191 192 193 194 195 196 197
        qstring_append(str, "\"");
        break;
    }
    case QTYPE_QDICT: {
        ToJsonIterState s;
        QDict *val = qobject_to_qdict(obj);

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

        s.count = 0;
        s.str = str;
217 218
        s.indent = indent + 1;
        s.pretty = pretty;
219 220
        qstring_append(str, "[");
        qlist_iter(val, (void *)to_json_list_iter, &s);
221 222 223 224 225 226
        if (pretty) {
            int j;
            qstring_append(str, "\n");
            for (j = 0 ; j < indent ; j++)
                qstring_append(str, "    ");
        }
227 228 229 230 231 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
        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 已提交
259 260
    case QTYPE_QERROR:
        /* XXX: should QError be emitted? */
261 262 263 264 265 266 267 268 269
    case QTYPE_NONE:
        break;
    }
}

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

270 271 272 273 274 275 276 277 278 279
    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);
280 281 282

    return str;
}