master
c 250 lines 6.61 KB
Raw
1 /*
2 * JSON Writer
3 *
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2010-2020 Red Hat Inc.
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
12 * See the COPYING.LIB file in the top-level directory.
13 *
14 */
15
16 #include "qemu/osdep.h"
17 #include "qobject/json-writer.h"
18 #include "qemu/unicode.h"
19
20 struct JSONWriter {
21 bool pretty;
22 bool need_comma;
23 GString *contents;
24 GByteArray *container_is_array;
25 };
26
27 /* Should cover most QMP responses without reallocation */
28 #define JSON_WRITER_INITIAL_SIZE 4096
29
30 JSONWriter *json_writer_new(bool pretty)
31 {
32 JSONWriter *writer = g_new(JSONWriter, 1);
33
34 writer->pretty = pretty;
35 writer->need_comma = false;
36 writer->contents = g_string_sized_new(JSON_WRITER_INITIAL_SIZE);
37 writer->container_is_array = g_byte_array_new();
38 return writer;
39 }
40
41 const char *json_writer_get(JSONWriter *writer)
42 {
43 g_assert(!writer->container_is_array->len);
44 return writer->contents->str;
45 }
46
47 GString *json_writer_get_and_free(JSONWriter *writer)
48 {
49 GString *contents = writer->contents;
50
51 writer->contents = NULL;
52 g_byte_array_free(writer->container_is_array, true);
53 g_free(writer);
54 return contents;
55 }
56
57 void json_writer_free(JSONWriter *writer)
58 {
59 if (writer) {
60 g_string_free(json_writer_get_and_free(writer), true);
61 }
62 }
63
64 static void enter_container(JSONWriter *writer, bool is_array)
65 {
66 unsigned depth = writer->container_is_array->len;
67
68 g_byte_array_set_size(writer->container_is_array, depth + 1);
69 writer->container_is_array->data[depth] = is_array;
70 writer->need_comma = false;
71 }
72
73 static void leave_container(JSONWriter *writer, bool is_array)
74 {
75 unsigned depth = writer->container_is_array->len;
76
77 assert(depth);
78 assert(writer->container_is_array->data[depth - 1] == is_array);
79 g_byte_array_set_size(writer->container_is_array, depth - 1);
80 writer->need_comma = true;
81 }
82
83 static bool in_object(JSONWriter *writer)
84 {
85 unsigned depth = writer->container_is_array->len;
86
87 return depth && !writer->container_is_array->data[depth - 1];
88 }
89
90 static void pretty_newline(JSONWriter *writer)
91 {
92 if (writer->pretty) {
93 g_string_append_printf(writer->contents, "\n%*s",
94 writer->container_is_array->len * 4, "");
95 }
96 }
97
98 static void pretty_newline_or_space(JSONWriter *writer)
99 {
100 if (writer->pretty) {
101 g_string_append_printf(writer->contents, "\n%*s",
102 writer->container_is_array->len * 4, "");
103 } else {
104 g_string_append_c(writer->contents, ' ');
105 }
106 }
107
108 static void quoted_str(JSONWriter *writer, const char *str)
109 {
110 const char *ptr;
111 char *end;
112 int cp;
113
114 g_string_append_c(writer->contents, '"');
115
116 for (ptr = str; *ptr; ptr = end) {
117 cp = mod_utf8_codepoint(ptr, 6, &end);
118 switch (cp) {
119 case '\"':
120 g_string_append(writer->contents, "\\\"");
121 break;
122 case '\\':
123 g_string_append(writer->contents, "\\\\");
124 break;
125 case '\b':
126 g_string_append(writer->contents, "\\b");
127 break;
128 case '\f':
129 g_string_append(writer->contents, "\\f");
130 break;
131 case '\n':
132 g_string_append(writer->contents, "\\n");
133 break;
134 case '\r':
135 g_string_append(writer->contents, "\\r");
136 break;
137 case '\t':
138 g_string_append(writer->contents, "\\t");
139 break;
140 default:
141 if (cp < 0) {
142 cp = 0xFFFD; /* replacement character */
143 }
144 if (cp > 0xFFFF) {
145 /* beyond BMP; need a surrogate pair */
146 g_string_append_printf(writer->contents, "\\u%04X\\u%04X",
147 0xD800 + ((cp - 0x10000) >> 10),
148 0xDC00 + ((cp - 0x10000) & 0x3FF));
149 } else if (cp < 0x20 || cp >= 0x7F) {
150 g_string_append_printf(writer->contents, "\\u%04X", cp);
151 } else {
152 g_string_append_c(writer->contents, cp);
153 }
154 }
155 };
156
157 g_string_append_c(writer->contents, '"');
158 }
159
160 static void maybe_comma_name(JSONWriter *writer, const char *name)
161 {
162 if (writer->need_comma) {
163 g_string_append_c(writer->contents, ',');
164 pretty_newline_or_space(writer);
165 } else {
166 if (writer->contents->len) {
167 pretty_newline(writer);
168 }
169 writer->need_comma = true;
170 }
171
172 if (in_object(writer)) {
173 quoted_str(writer, name);
174 g_string_append(writer->contents, ": ");
175 }
176 }
177
178 void json_writer_start_object(JSONWriter *writer, const char *name)
179 {
180 maybe_comma_name(writer, name);
181 g_string_append_c(writer->contents, '{');
182 enter_container(writer, false);
183 }
184
185 void json_writer_end_object(JSONWriter *writer)
186 {
187 leave_container(writer, false);
188 pretty_newline(writer);
189 g_string_append_c(writer->contents, '}');
190 }
191
192 void json_writer_start_array(JSONWriter *writer, const char *name)
193 {
194 maybe_comma_name(writer, name);
195 g_string_append_c(writer->contents, '[');
196 enter_container(writer, true);
197 }
198
199 void json_writer_end_array(JSONWriter *writer)
200 {
201 leave_container(writer, true);
202 pretty_newline(writer);
203 g_string_append_c(writer->contents, ']');
204 }
205
206 void json_writer_bool(JSONWriter *writer, const char *name, bool val)
207 {
208 maybe_comma_name(writer, name);
209 g_string_append(writer->contents, val ? "true" : "false");
210 }
211
212 void json_writer_null(JSONWriter *writer, const char *name)
213 {
214 maybe_comma_name(writer, name);
215 g_string_append(writer->contents, "null");
216 }
217
218 void json_writer_int64(JSONWriter *writer, const char *name, int64_t val)
219 {
220 maybe_comma_name(writer, name);
221 g_string_append_printf(writer->contents, "%" PRId64, val);
222 }
223
224 void json_writer_uint64(JSONWriter *writer, const char *name, uint64_t val)
225 {
226 maybe_comma_name(writer, name);
227 g_string_append_printf(writer->contents, "%" PRIu64, val);
228 }
229
230 void json_writer_double(JSONWriter *writer, const char *name, double val)
231 {
232 maybe_comma_name(writer, name);
233
234 /*
235 * FIXME: g_string_append_printf() is locale dependent; but JSON
236 * requires numbers to be formatted as if in the C locale.
237 * Dependence on C locale is a pervasive issue in QEMU.
238 */
239 /*
240 * FIXME: This risks printing Inf or NaN, which are not valid
241 * JSON values.
242 */
243 g_string_append_printf(writer->contents, "%.17g", val);
244 }
245
246 void json_writer_str(JSONWriter *writer, const char *name, const char *str)
247 {
248 maybe_comma_name(writer, name);
249 quoted_str(writer, str);
250 }