qjson.c 7.4 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 139 140 141 142 143 144 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 173 174
{
    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;

        ptr = qstring_get_str(val);
        qstring_append(str, "\"");
        while (*ptr) {
            if ((ptr[0] & 0xE0) == 0xE0 &&
                (ptr[1] & 0x80) && (ptr[2] & 0x80)) {
                uint16_t wchar;
                char escape[7];

                wchar  = (ptr[0] & 0x0F) << 12;
                wchar |= (ptr[1] & 0x3F) << 6;
                wchar |= (ptr[2] & 0x3F);
                ptr += 2;

                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
                qstring_append(str, escape);
            } else if ((ptr[0] & 0xE0) == 0xC0 && (ptr[1] & 0x80)) {
                uint16_t wchar;
                char escape[7];

                wchar  = (ptr[0] & 0x1F) << 6;
                wchar |= (ptr[1] & 0x3F);
                ptr++;

                snprintf(escape, sizeof(escape), "\\u%04X", wchar);
                qstring_append(str, escape);
            } else switch (ptr[0]) {
                case '\"':
                    qstring_append(str, "\\\"");
                    break;
                case '\\':
                    qstring_append(str, "\\\\");
                    break;
                case '\b':
                    qstring_append(str, "\\b");
                    break;
L
Luiz Capitulino 已提交
175 176 177
                case '\f':
                    qstring_append(str, "\\f");
                    break;
178 179 180 181 182 183 184 185 186 187
                case '\n':
                    qstring_append(str, "\\n");
                    break;
                case '\r':
                    qstring_append(str, "\\r");
                    break;
                case '\t':
                    qstring_append(str, "\\t");
                    break;
                default: {
188 189 190 191 192 193 194 195
                    if (ptr[0] <= 0x1F) {
                        char escape[7];
                        snprintf(escape, sizeof(escape), "\\u%04X", ptr[0]);
                        qstring_append(str, escape);
                    } else {
                        char buf[2] = { ptr[0], 0 };
                        qstring_append(str, buf);
                    }
196 197 198 199 200 201 202 203 204 205 206 207 208 209
                    break;
                }
                }
            ptr++;
        }
        qstring_append(str, "\"");
        break;
    }
    case QTYPE_QDICT: {
        ToJsonIterState s;
        QDict *val = qobject_to_qdict(obj);

        s.count = 0;
        s.str = str;
210 211
        s.indent = indent + 1;
        s.pretty = pretty;
212 213
        qstring_append(str, "{");
        qdict_iter(val, to_json_dict_iter, &s);
214 215 216 217 218 219
        if (pretty) {
            int j;
            qstring_append(str, "\n");
            for (j = 0 ; j < indent ; j++)
                qstring_append(str, "    ");
        }
220 221 222 223 224 225 226 227 228
        qstring_append(str, "}");
        break;
    }
    case QTYPE_QLIST: {
        ToJsonIterState s;
        QList *val = qobject_to_qlist(obj);

        s.count = 0;
        s.str = str;
229 230
        s.indent = indent + 1;
        s.pretty = pretty;
231 232
        qstring_append(str, "[");
        qlist_iter(val, (void *)to_json_list_iter, &s);
233 234 235 236 237 238
        if (pretty) {
            int j;
            qstring_append(str, "\n");
            for (j = 0 ; j < indent ; j++)
                qstring_append(str, "    ");
        }
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 264 265 266 267 268 269 270
        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 已提交
271 272
    case QTYPE_QERROR:
        /* XXX: should QError be emitted? */
273 274 275 276 277 278 279 280 281
    case QTYPE_NONE:
        break;
    }
}

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

282 283 284 285 286 287 288 289 290 291
    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);
292 293 294

    return str;
}