blob: 9e6b96004dc71be9dc96e85a6d4279f508c12765 [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é Fonsecaa15b7fe2011-07-16 21:08:16 -070038#include <ostream>
39#include <string>
José Fonseca589082d2011-03-30 09:10:40 +010040
41
42class JSONWriter
43{
44private:
45 std::ostream &os;
46
47 int level;
48 bool value;
José Fonsecafc92b762011-04-08 09:44:26 +010049 char space;
José Fonseca589082d2011-03-30 09:10:40 +010050
51 void newline(void) {
52 os << "\n";
53 for (int i = 0; i < level; ++i)
54 os << " ";
55 }
56
57 void separator(void) {
58 if (value) {
59 os << ",";
José Fonsecafc92b762011-04-08 09:44:26 +010060 switch (space) {
61 case '\0':
62 break;
63 case '\n':
64 newline();
65 break;
66 default:
67 os << space;
68 break;
69 }
José Fonseca2e8febd2011-04-09 00:10:30 +010070 } else {
71 if (space == '\n') {
72 newline();
73 }
José Fonseca589082d2011-03-30 09:10:40 +010074 }
75 }
76
José Fonseca702c1c62011-04-08 07:58:05 +010077 void escapeAsciiString(const char *str) {
José Fonseca589082d2011-03-30 09:10:40 +010078 os << "\"";
José Fonseca702c1c62011-04-08 07:58:05 +010079
80 const unsigned char *src = (const unsigned char *)str;
José Fonseca589082d2011-03-30 09:10:40 +010081 unsigned char c;
José Fonseca702c1c62011-04-08 07:58:05 +010082 while ((c = *src++)) {
83 if ((c == '\"') ||
84 (c == '\\')) {
85 // escape character
86 os << '\\' << (unsigned char)c;
87 } else if ((c >= 0x20 && c <= 0x7e) ||
88 c == '\t' ||
89 c == '\r' ||
90 c == '\n') {
91 // pass-through character
92 os << (unsigned char)c;
93 } else {
94 assert(0);
95 os << "?";
José Fonseca589082d2011-03-30 09:10:40 +010096 }
97 }
José Fonseca702c1c62011-04-08 07:58:05 +010098
99 os << "\"";
100 }
101
102 void escapeUnicodeString(const char *str) {
103 os << "\"";
104
105 const char *locale = setlocale(LC_CTYPE, "");
106 const char *src = str;
107 mbstate_t state;
108
109 memset(&state, 0, sizeof state);
110
111 do {
112 // Convert characters one at a time in order to recover from
113 // conversion errors
114 wchar_t c;
115 size_t written = mbsrtowcs(&c, &src, 1, &state);
116 if (written == 0) {
117 // completed
118 break;
119 } if (written == (size_t)-1) {
120 // conversion error -- skip
121 os << "?";
122 do {
123 ++src;
124 } while (*src & 0x80);
125 } else if ((c == '\"') ||
126 (c == '\\')) {
127 // escape character
128 os << '\\' << (unsigned char)c;
129 } else if ((c >= 0x20 && c <= 0x7e) ||
130 c == '\t' ||
131 c == '\r' ||
132 c == '\n') {
133 // pass-through character
134 os << (unsigned char)c;
135 } else {
136 // unicode
137 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
138 os << std::dec;
139 }
140 } while (src);
141
142 setlocale(LC_CTYPE, locale);
143
José Fonseca589082d2011-03-30 09:10:40 +0100144 os << "\"";
145 }
146
José Fonseca23a17d62011-04-09 12:22:58 +0100147 void encodeBase64String(const unsigned char *bytes, size_t size) {
148 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
149 unsigned char c0, c1, c2, c3;
150 char buf[4];
151 unsigned written;
152
153 os << "\"";
154
155 written = 0;
156 while (size >= 3) {
157 c0 = bytes[0] >> 2;
158 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
159 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
160 c3 = bytes[2] & 0x3f;
161
162 buf[0] = table64[c0];
163 buf[1] = table64[c1];
164 buf[2] = table64[c2];
165 buf[3] = table64[c3];
166
167 os.write(buf, 4);
168
169 bytes += 3;
170 size -= 3;
171 ++written;
172
José Fonseca56478ca2011-04-09 12:38:10 +0100173 if (written >= 76/4 && size) {
José Fonseca23a17d62011-04-09 12:22:58 +0100174 os << "\n";
175 written = 0;
176 }
177 }
178
179 if (size > 0) {
180 c0 = bytes[0] >> 2;
181 c1 = ((bytes[0] & 0x03) << 4);
José Fonseca56478ca2011-04-09 12:38:10 +0100182 buf[2] = '=';
183 buf[3] = '=';
José Fonseca23a17d62011-04-09 12:22:58 +0100184
185 if (size > 1) {
186 c1 |= ((bytes[1] & 0xf0) >> 4);
187 c2 = ((bytes[1] & 0x0f) << 2);
188 if (size > 2) {
189 c2 |= ((bytes[2] & 0xc0) >> 6);
190 c3 = bytes[2] & 0x3f;
191 buf[3] = table64[c3];
José Fonseca23a17d62011-04-09 12:22:58 +0100192 }
193 buf[2] = table64[c2];
José Fonseca23a17d62011-04-09 12:22:58 +0100194 }
195 buf[1] = table64[c1];
196 buf[0] = table64[c0];
197
198 os.write(buf, 4);
199 }
200
201 os << "\"";
202 }
203
José Fonseca589082d2011-03-30 09:10:40 +0100204public:
205 JSONWriter(std::ostream &_os) :
206 os(_os),
207 level(0),
José Fonsecafc92b762011-04-08 09:44:26 +0100208 value(false),
209 space(0)
José Fonseca589082d2011-03-30 09:10:40 +0100210 {
211 beginObject();
212 }
213
214 ~JSONWriter() {
215 endObject();
216 newline();
217 }
218
219 inline void beginObject() {
José Fonseca3b4677e2011-04-07 10:14:02 +0100220 separator();
José Fonseca589082d2011-03-30 09:10:40 +0100221 os << "{";
222 ++level;
223 value = false;
224 }
225
226 inline void endObject() {
227 --level;
228 if (value)
229 newline();
230 os << "}";
231 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100232 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100233 }
234
235 inline void beginMember(const char * name) {
José Fonsecafc92b762011-04-08 09:44:26 +0100236 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100237 separator();
238 newline();
José Fonseca702c1c62011-04-08 07:58:05 +0100239 escapeAsciiString(name);
José Fonseca589082d2011-03-30 09:10:40 +0100240 os << ": ";
241 value = false;
242 }
243
José Fonsecaa15b7fe2011-07-16 21:08:16 -0700244 inline void beginMember(const std::string &name) {
245 beginMember(name.c_str());
246 }
247
José Fonseca589082d2011-03-30 09:10:40 +0100248 inline void endMember(void) {
249 assert(value);
250 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100251 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100252 }
253
254 inline void beginArray() {
255 separator();
256 os << "[";
José Fonsecafc92b762011-04-08 09:44:26 +0100257 ++level;
José Fonseca589082d2011-03-30 09:10:40 +0100258 value = false;
José Fonseca2e8febd2011-04-09 00:10:30 +0100259 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100260 }
261
262 inline void endArray(void) {
José Fonsecafc92b762011-04-08 09:44:26 +0100263 --level;
José Fonseca2e8febd2011-04-09 00:10:30 +0100264 if (space == '\n') {
265 newline();
266 }
José Fonseca589082d2011-03-30 09:10:40 +0100267 os << "]";
268 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100269 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100270 }
271
272 inline void writeString(const char *s) {
273 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100274 escapeUnicodeString(s);
José Fonsecaed2167c2011-03-31 01:15:23 +0100275 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100276 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100277 }
278
José Fonsecaa15b7fe2011-07-16 21:08:16 -0700279 inline void writeString(const std::string &s) {
280 writeString(s.c_str());
281 }
282
José Fonseca23a17d62011-04-09 12:22:58 +0100283 inline void writeBase64(const void *bytes, size_t size) {
284 separator();
285 encodeBase64String((const unsigned char *)bytes, size);
286 value = true;
287 space = ' ';
288 }
289
José Fonseca589082d2011-03-30 09:10:40 +0100290 inline void writeNull(void) {
291 separator();
292 os << "null";
293 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100294 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100295 }
296
297 inline void writeBool(bool b) {
298 separator();
299 os << (b ? "true" : "false");
300 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100301 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100302 }
303
304 template<class T>
José Fonseca23a17d62011-04-09 12:22:58 +0100305 inline void writeNumber(T n) {
José Fonseca364a5a62011-05-06 20:49:45 +0100306 if (n != n) {
307 // NaN
308 writeNull();
309 } else {
310 separator();
311 os << std::dec << std::setprecision(9) << n;
312 value = true;
313 space = ' ';
314 }
José Fonseca589082d2011-03-30 09:10:40 +0100315 }
José Fonseca23a17d62011-04-09 12:22:58 +0100316
317 inline void writeStringMember(const char *name, const char *s) {
318 beginMember(name);
319 writeString(s);
320 endMember();
321 }
322
323 inline void writeBoolMember(const char *name, bool b) {
324 beginMember(name);
325 writeBool(b);
326 endMember();
327 }
328
329 template<class T>
330 inline void writeNumberMember(const char *name, T n) {
331 beginMember(name);
332 writeNumber(n);
333 endMember();
334 }
José Fonseca589082d2011-03-30 09:10:40 +0100335};
336
337#endif /* _JSON_HPP_ */