aio-epoll: Fix use-after-free of node
[qemu.git] / qjson.c
blobe478802a461f6b5556732d8a07b5066780c1c524
1 /*
2 * QEMU JSON writer
4 * Copyright Alexander Graf
6 * Authors:
7 * Alexander Graf <agraf@suse.de>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include <qapi/qmp/qstring.h>
15 #include <stdbool.h>
16 #include <glib.h>
17 #include <qjson.h>
18 #include <qemu/module.h>
19 #include <qom/object.h>
21 struct QJSON {
22 Object obj;
23 QString *str;
24 bool omit_comma;
27 #define QJSON(obj) OBJECT_CHECK(QJSON, (obj), TYPE_QJSON)
29 static void json_emit_element(QJSON *json, const char *name)
31 /* Check whether we need to print a , before an element */
32 if (json->omit_comma) {
33 json->omit_comma = false;
34 } else {
35 qstring_append(json->str, ", ");
38 if (name) {
39 qstring_append(json->str, "\"");
40 qstring_append(json->str, name);
41 qstring_append(json->str, "\" : ");
45 void json_start_object(QJSON *json, const char *name)
47 json_emit_element(json, name);
48 qstring_append(json->str, "{ ");
49 json->omit_comma = true;
52 void json_end_object(QJSON *json)
54 qstring_append(json->str, " }");
55 json->omit_comma = false;
58 void json_start_array(QJSON *json, const char *name)
60 json_emit_element(json, name);
61 qstring_append(json->str, "[ ");
62 json->omit_comma = true;
65 void json_end_array(QJSON *json)
67 qstring_append(json->str, " ]");
68 json->omit_comma = false;
71 void json_prop_int(QJSON *json, const char *name, int64_t val)
73 json_emit_element(json, name);
74 qstring_append_int(json->str, val);
77 void json_prop_str(QJSON *json, const char *name, const char *str)
79 json_emit_element(json, name);
80 qstring_append_chr(json->str, '"');
81 qstring_append(json->str, str);
82 qstring_append_chr(json->str, '"');
85 const char *qjson_get_str(QJSON *json)
87 return qstring_get_str(json->str);
90 QJSON *qjson_new(void)
92 QJSON *json = QJSON(object_new(TYPE_QJSON));
93 return json;
96 void qjson_finish(QJSON *json)
98 json_end_object(json);
101 static void qjson_initfn(Object *obj)
103 QJSON *json = QJSON(obj);
105 json->str = qstring_from_str("{ ");
106 json->omit_comma = true;
109 static void qjson_finalizefn(Object *obj)
111 QJSON *json = QJSON(obj);
113 qobject_decref(QOBJECT(json->str));
116 static const TypeInfo qjson_type_info = {
117 .name = TYPE_QJSON,
118 .parent = TYPE_OBJECT,
119 .instance_size = sizeof(QJSON),
120 .instance_init = qjson_initfn,
121 .instance_finalize = qjson_finalizefn,
124 static void qjson_register_types(void)
126 type_register_static(&qjson_type_info);
129 type_init(qjson_register_types)