blob: 6af1f4c6c4fab898fae8ca730c5092cefc850459 [file] [log] [blame]
José Fonseca589082d2011-03-30 09:10:40 +01001/**************************************************************************
2 *
3 * Copyright 2011 Jose Fonseca
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26/*
27 * Trace writing functions.
28 */
29
30#ifndef _JSON_HPP_
31#define _JSON_HPP_
32
33#include <assert.h>
34#include <stddef.h>
José Fonseca702c1c62011-04-08 07:58:05 +010035#include <wchar.h>
José Fonseca589082d2011-03-30 09:10:40 +010036
José Fonseca702c1c62011-04-08 07:58:05 +010037#include <iomanip>
José Fonseca5f2245e2012-05-14 20:20:44 +010038#include <limits>
José Fonsecaa15b7fe2011-07-16 21:08:16 -070039#include <ostream>
40#include <string>
José Fonseca589082d2011-03-30 09:10:40 +010041
42
43class JSONWriter
44{
45private:
46 std::ostream &os;
47
48 int level;
49 bool value;
José Fonsecafc92b762011-04-08 09:44:26 +010050 char space;
José Fonseca589082d2011-03-30 09:10:40 +010051
52 void newline(void) {
53 os << "\n";
54 for (int i = 0; i < level; ++i)
55 os << " ";
56 }
57
58 void separator(void) {
59 if (value) {
60 os << ",";
José Fonsecafc92b762011-04-08 09:44:26 +010061 switch (space) {
62 case '\0':
63 break;
64 case '\n':
65 newline();
66 break;
67 default:
68 os << space;
69 break;
70 }
José Fonseca2e8febd2011-04-09 00:10:30 +010071 } else {
72 if (space == '\n') {
73 newline();
74 }
José Fonseca589082d2011-03-30 09:10:40 +010075 }
76 }
77
José Fonseca702c1c62011-04-08 07:58:05 +010078 void escapeAsciiString(const char *str) {
José Fonseca589082d2011-03-30 09:10:40 +010079 os << "\"";
José Fonseca702c1c62011-04-08 07:58:05 +010080
81 const unsigned char *src = (const unsigned char *)str;
José Fonseca589082d2011-03-30 09:10:40 +010082 unsigned char c;
José Fonseca702c1c62011-04-08 07:58:05 +010083 while ((c = *src++)) {
84 if ((c == '\"') ||
85 (c == '\\')) {
86 // escape character
87 os << '\\' << (unsigned char)c;
88 } else if ((c >= 0x20 && c <= 0x7e) ||
89 c == '\t' ||
90 c == '\r' ||
91 c == '\n') {
92 // pass-through character
93 os << (unsigned char)c;
94 } else {
95 assert(0);
96 os << "?";
José Fonseca589082d2011-03-30 09:10:40 +010097 }
98 }
José Fonseca702c1c62011-04-08 07:58:05 +010099
100 os << "\"";
101 }
102
103 void escapeUnicodeString(const char *str) {
104 os << "\"";
105
106 const char *locale = setlocale(LC_CTYPE, "");
107 const char *src = str;
108 mbstate_t state;
109
110 memset(&state, 0, sizeof state);
111
112 do {
113 // Convert characters one at a time in order to recover from
114 // conversion errors
115 wchar_t c;
116 size_t written = mbsrtowcs(&c, &src, 1, &state);
117 if (written == 0) {
118 // completed
119 break;
120 } if (written == (size_t)-1) {
121 // conversion error -- skip
122 os << "?";
123 do {
124 ++src;
125 } while (*src & 0x80);
126 } else if ((c == '\"') ||
127 (c == '\\')) {
128 // escape character
129 os << '\\' << (unsigned char)c;
130 } else if ((c >= 0x20 && c <= 0x7e) ||
131 c == '\t' ||
132 c == '\r' ||
133 c == '\n') {
134 // pass-through character
135 os << (unsigned char)c;
136 } else {
137 // unicode
138 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
139 os << std::dec;
140 }
141 } while (src);
142
143 setlocale(LC_CTYPE, locale);
144
José Fonseca589082d2011-03-30 09:10:40 +0100145 os << "\"";
146 }
147
José Fonseca23a17d62011-04-09 12:22:58 +0100148 void encodeBase64String(const unsigned char *bytes, size_t size) {
149 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
150 unsigned char c0, c1, c2, c3;
151 char buf[4];
152 unsigned written;
153
154 os << "\"";
155
156 written = 0;
157 while (size >= 3) {
158 c0 = bytes[0] >> 2;
159 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
160 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
161 c3 = bytes[2] & 0x3f;
162
163 buf[0] = table64[c0];
164 buf[1] = table64[c1];
165 buf[2] = table64[c2];
166 buf[3] = table64[c3];
167
168 os.write(buf, 4);
169
170 bytes += 3;
171 size -= 3;
172 ++written;
173
José Fonseca56478ca2011-04-09 12:38:10 +0100174 if (written >= 76/4 && size) {
José Fonseca23a17d62011-04-09 12:22:58 +0100175 os << "\n";
176 written = 0;
177 }
178 }
179
180 if (size > 0) {
181 c0 = bytes[0] >> 2;
182 c1 = ((bytes[0] & 0x03) << 4);
José Fonseca56478ca2011-04-09 12:38:10 +0100183 buf[2] = '=';
184 buf[3] = '=';
José Fonseca23a17d62011-04-09 12:22:58 +0100185
186 if (size > 1) {
187 c1 |= ((bytes[1] & 0xf0) >> 4);
188 c2 = ((bytes[1] & 0x0f) << 2);
189 if (size > 2) {
190 c2 |= ((bytes[2] & 0xc0) >> 6);
191 c3 = bytes[2] & 0x3f;
192 buf[3] = table64[c3];
José Fonseca23a17d62011-04-09 12:22:58 +0100193 }
194 buf[2] = table64[c2];
José Fonseca23a17d62011-04-09 12:22:58 +0100195 }
196 buf[1] = table64[c1];
197 buf[0] = table64[c0];
198
199 os.write(buf, 4);
200 }
201
202 os << "\"";
203 }
204
José Fonseca589082d2011-03-30 09:10:40 +0100205public:
206 JSONWriter(std::ostream &_os) :
207 os(_os),
208 level(0),
José Fonsecafc92b762011-04-08 09:44:26 +0100209 value(false),
210 space(0)
José Fonseca589082d2011-03-30 09:10:40 +0100211 {
212 beginObject();
213 }
214
215 ~JSONWriter() {
216 endObject();
217 newline();
218 }
219
220 inline void beginObject() {
José Fonseca3b4677e2011-04-07 10:14:02 +0100221 separator();
José Fonseca589082d2011-03-30 09:10:40 +0100222 os << "{";
223 ++level;
224 value = false;
225 }
226
227 inline void endObject() {
228 --level;
229 if (value)
230 newline();
231 os << "}";
232 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100233 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100234 }
235
236 inline void beginMember(const char * name) {
José Fonsecafc92b762011-04-08 09:44:26 +0100237 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100238 separator();
239 newline();
José Fonseca702c1c62011-04-08 07:58:05 +0100240 escapeAsciiString(name);
José Fonseca589082d2011-03-30 09:10:40 +0100241 os << ": ";
242 value = false;
243 }
244
José Fonsecaa15b7fe2011-07-16 21:08:16 -0700245 inline void beginMember(const std::string &name) {
246 beginMember(name.c_str());
247 }
248
José Fonseca589082d2011-03-30 09:10:40 +0100249 inline void endMember(void) {
250 assert(value);
251 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100252 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100253 }
254
255 inline void beginArray() {
256 separator();
257 os << "[";
José Fonsecafc92b762011-04-08 09:44:26 +0100258 ++level;
José Fonseca589082d2011-03-30 09:10:40 +0100259 value = false;
José Fonseca2e8febd2011-04-09 00:10:30 +0100260 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100261 }
262
263 inline void endArray(void) {
José Fonsecafc92b762011-04-08 09:44:26 +0100264 --level;
José Fonseca2e8febd2011-04-09 00:10:30 +0100265 if (space == '\n') {
266 newline();
267 }
José Fonseca589082d2011-03-30 09:10:40 +0100268 os << "]";
269 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100270 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100271 }
272
273 inline void writeString(const char *s) {
José Fonseca02bf5b42011-10-11 19:33:02 +0100274 if (!s) {
275 writeNull();
276 return;
277 }
278
José Fonseca589082d2011-03-30 09:10:40 +0100279 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100280 escapeUnicodeString(s);
José Fonsecaed2167c2011-03-31 01:15:23 +0100281 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100282 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100283 }
284
José Fonsecaa15b7fe2011-07-16 21:08:16 -0700285 inline void writeString(const std::string &s) {
286 writeString(s.c_str());
287 }
288
José Fonseca23a17d62011-04-09 12:22:58 +0100289 inline void writeBase64(const void *bytes, size_t size) {
290 separator();
291 encodeBase64String((const unsigned char *)bytes, size);
292 value = true;
293 space = ' ';
294 }
295
José Fonseca589082d2011-03-30 09:10:40 +0100296 inline void writeNull(void) {
297 separator();
298 os << "null";
299 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100300 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100301 }
302
303 inline void writeBool(bool b) {
304 separator();
305 os << (b ? "true" : "false");
306 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100307 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100308 }
309
José Fonseca7ec90502012-04-16 20:09:42 +0100310
311 /**
312 * Special case for char to prevent it to be written as a literal
313 * character.
314 */
315 inline void writeNumber(char n) {
316 separator();
317 os << std::dec << static_cast<int>(n);
318 value = true;
319 space = ' ';
320 }
321
322 inline void writeNumber(unsigned char n) {
323 separator();
324 os << std::dec << static_cast<unsigned>(n);
325 value = true;
326 space = ' ';
327 }
328
José Fonseca589082d2011-03-30 09:10:40 +0100329 template<class T>
José Fonseca23a17d62011-04-09 12:22:58 +0100330 inline void writeNumber(T n) {
José Fonseca364a5a62011-05-06 20:49:45 +0100331 if (n != n) {
332 // NaN
333 writeNull();
334 } else {
335 separator();
José Fonseca5f2245e2012-05-14 20:20:44 +0100336 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
José Fonseca364a5a62011-05-06 20:49:45 +0100337 value = true;
338 space = ' ';
339 }
José Fonseca589082d2011-03-30 09:10:40 +0100340 }
José Fonseca23a17d62011-04-09 12:22:58 +0100341
342 inline void writeStringMember(const char *name, const char *s) {
343 beginMember(name);
344 writeString(s);
345 endMember();
346 }
347
348 inline void writeBoolMember(const char *name, bool b) {
349 beginMember(name);
350 writeBool(b);
351 endMember();
352 }
353
354 template<class T>
355 inline void writeNumberMember(const char *name, T n) {
356 beginMember(name);
357 writeNumber(n);
358 endMember();
359 }
José Fonseca589082d2011-03-30 09:10:40 +0100360};
361
362#endif /* _JSON_HPP_ */