AHCI: Protect cmd register
[qemu/ar7.git] / qjson.c
blob0cda2690f51c33083b3278f98ae8fb2878c61af6
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 static void json_emit_element(QJSON *json, const char *name)
29 /* Check whether we need to print a , before an element */
30 if (json->omit_comma) {
31 json->omit_comma = false;
32 } else {
33 qstring_append(json->str, ", ");
36 if (name) {
37 qstring_append(json->str, "\"");
38 qstring_append(json->str, name);
39 qstring_append(json->str, "\" : ");
43 void json_start_object(QJSON *json, const char *name)
45 json_emit_element(json, name);
46 qstring_append(json->str, "{ ");
47 json->omit_comma = true;
50 void json_end_object(QJSON *json)
52 qstring_append(json->str, " }");
53 json->omit_comma = false;
56 void json_start_array(QJSON *json, const char *name)
58 json_emit_element(json, name);
59 qstring_append(json->str, "[ ");
60 json->omit_comma = true;
63 void json_end_array(QJSON *json)
65 qstring_append(json->str, " ]");
66 json->omit_comma = false;
69 void json_prop_int(QJSON *json, const char *name, int64_t val)
71 json_emit_element(json, name);
72 qstring_append_int(json->str, val);
75 void json_prop_str(QJSON *json, const char *name, const char *str)
77 json_emit_element(json, name);
78 qstring_append_chr(json->str, '"');
79 qstring_append(json->str, str);
80 qstring_append_chr(json->str, '"');
83 const char *qjson_get_str(QJSON *json)
85 return qstring_get_str(json->str);
88 QJSON *qjson_new(void)
90 QJSON *json = (QJSON *)object_new(TYPE_QJSON);
91 return json;
94 void qjson_finish(QJSON *json)
96 json_end_object(json);
99 static void qjson_initfn(Object *obj)
101 QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
102 assert(json);
104 json->str = qstring_from_str("{ ");
105 json->omit_comma = true;
108 static void qjson_finalizefn(Object *obj)
110 QJSON *json = (QJSON *)object_dynamic_cast(obj, TYPE_QJSON);
112 assert(json);
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)