blob: 9c491c552dcab93b94f09b6bef00a1636f0f7c8c [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
37#include <ostream>
José Fonseca702c1c62011-04-08 07:58:05 +010038#include <iomanip>
José Fonseca589082d2011-03-30 09:10:40 +010039
40
41class JSONWriter
42{
43private:
44 std::ostream &os;
45
46 int level;
47 bool value;
José Fonsecafc92b762011-04-08 09:44:26 +010048 char space;
José Fonseca589082d2011-03-30 09:10:40 +010049
50 void newline(void) {
51 os << "\n";
52 for (int i = 0; i < level; ++i)
53 os << " ";
54 }
55
56 void separator(void) {
57 if (value) {
58 os << ",";
José Fonsecafc92b762011-04-08 09:44:26 +010059 switch (space) {
60 case '\0':
61 break;
62 case '\n':
63 newline();
64 break;
65 default:
66 os << space;
67 break;
68 }
José Fonseca2e8febd2011-04-09 00:10:30 +010069 } else {
70 if (space == '\n') {
71 newline();
72 }
José Fonseca589082d2011-03-30 09:10:40 +010073 }
74 }
75
José Fonseca702c1c62011-04-08 07:58:05 +010076 void escapeAsciiString(const char *str) {
José Fonseca589082d2011-03-30 09:10:40 +010077 os << "\"";
José Fonseca702c1c62011-04-08 07:58:05 +010078
79 const unsigned char *src = (const unsigned char *)str;
José Fonseca589082d2011-03-30 09:10:40 +010080 unsigned char c;
José Fonseca702c1c62011-04-08 07:58:05 +010081 while ((c = *src++)) {
82 if ((c == '\"') ||
83 (c == '\\')) {
84 // escape character
85 os << '\\' << (unsigned char)c;
86 } else if ((c >= 0x20 && c <= 0x7e) ||
87 c == '\t' ||
88 c == '\r' ||
89 c == '\n') {
90 // pass-through character
91 os << (unsigned char)c;
92 } else {
93 assert(0);
94 os << "?";
José Fonseca589082d2011-03-30 09:10:40 +010095 }
96 }
José Fonseca702c1c62011-04-08 07:58:05 +010097
98 os << "\"";
99 }
100
101 void escapeUnicodeString(const char *str) {
102 os << "\"";
103
104 const char *locale = setlocale(LC_CTYPE, "");
105 const char *src = str;
106 mbstate_t state;
107
108 memset(&state, 0, sizeof state);
109
110 do {
111 // Convert characters one at a time in order to recover from
112 // conversion errors
113 wchar_t c;
114 size_t written = mbsrtowcs(&c, &src, 1, &state);
115 if (written == 0) {
116 // completed
117 break;
118 } if (written == (size_t)-1) {
119 // conversion error -- skip
120 os << "?";
121 do {
122 ++src;
123 } while (*src & 0x80);
124 } else if ((c == '\"') ||
125 (c == '\\')) {
126 // escape character
127 os << '\\' << (unsigned char)c;
128 } else if ((c >= 0x20 && c <= 0x7e) ||
129 c == '\t' ||
130 c == '\r' ||
131 c == '\n') {
132 // pass-through character
133 os << (unsigned char)c;
134 } else {
135 // unicode
136 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
137 os << std::dec;
138 }
139 } while (src);
140
141 setlocale(LC_CTYPE, locale);
142
José Fonseca589082d2011-03-30 09:10:40 +0100143 os << "\"";
144 }
145
José Fonseca23a17d62011-04-09 12:22:58 +0100146 void encodeBase64String(const unsigned char *bytes, size_t size) {
147 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
148 unsigned char c0, c1, c2, c3;
149 char buf[4];
150 unsigned written;
151
152 os << "\"";
153
154 written = 0;
155 while (size >= 3) {
156 c0 = bytes[0] >> 2;
157 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
158 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
159 c3 = bytes[2] & 0x3f;
160
161 buf[0] = table64[c0];
162 buf[1] = table64[c1];
163 buf[2] = table64[c2];
164 buf[3] = table64[c3];
165
166 os.write(buf, 4);
167
168 bytes += 3;
169 size -= 3;
170 ++written;
171
172 if (written >= 76/4) {
173 os << "\n";
174 written = 0;
175 }
176 }
177
178 if (size > 0) {
179 c0 = bytes[0] >> 2;
180 c1 = ((bytes[0] & 0x03) << 4);
181
182 if (size > 1) {
183 c1 |= ((bytes[1] & 0xf0) >> 4);
184 c2 = ((bytes[1] & 0x0f) << 2);
185 if (size > 2) {
186 c2 |= ((bytes[2] & 0xc0) >> 6);
187 c3 = bytes[2] & 0x3f;
188 buf[3] = table64[c3];
189 } else {
190 buf[3] = '=';
191 }
192 buf[2] = table64[c2];
193 } else {
194 buf[3] = '=';
195 buf[2] = '=';
196 }
197 buf[1] = table64[c1];
198 buf[0] = table64[c0];
199
200 os.write(buf, 4);
201 }
202
203 os << "\"";
204 }
205
José Fonseca589082d2011-03-30 09:10:40 +0100206public:
207 JSONWriter(std::ostream &_os) :
208 os(_os),
209 level(0),
José Fonsecafc92b762011-04-08 09:44:26 +0100210 value(false),
211 space(0)
José Fonseca589082d2011-03-30 09:10:40 +0100212 {
213 beginObject();
214 }
215
216 ~JSONWriter() {
217 endObject();
218 newline();
219 }
220
221 inline void beginObject() {
José Fonseca3b4677e2011-04-07 10:14:02 +0100222 separator();
José Fonseca589082d2011-03-30 09:10:40 +0100223 os << "{";
224 ++level;
225 value = false;
226 }
227
228 inline void endObject() {
229 --level;
230 if (value)
231 newline();
232 os << "}";
233 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100234 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100235 }
236
237 inline void beginMember(const char * name) {
José Fonsecafc92b762011-04-08 09:44:26 +0100238 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100239 separator();
240 newline();
José Fonseca702c1c62011-04-08 07:58:05 +0100241 escapeAsciiString(name);
José Fonseca589082d2011-03-30 09:10:40 +0100242 os << ": ";
243 value = false;
244 }
245
246 inline void endMember(void) {
247 assert(value);
248 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100249 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100250 }
251
252 inline void beginArray() {
253 separator();
254 os << "[";
José Fonsecafc92b762011-04-08 09:44:26 +0100255 ++level;
José Fonseca589082d2011-03-30 09:10:40 +0100256 value = false;
José Fonseca2e8febd2011-04-09 00:10:30 +0100257 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100258 }
259
260 inline void endArray(void) {
José Fonsecafc92b762011-04-08 09:44:26 +0100261 --level;
José Fonseca2e8febd2011-04-09 00:10:30 +0100262 if (space == '\n') {
263 newline();
264 }
José Fonseca589082d2011-03-30 09:10:40 +0100265 os << "]";
266 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100267 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100268 }
269
270 inline void writeString(const char *s) {
271 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100272 escapeUnicodeString(s);
José Fonsecaed2167c2011-03-31 01:15:23 +0100273 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100274 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100275 }
276
José Fonseca23a17d62011-04-09 12:22:58 +0100277 inline void writeBase64(const void *bytes, size_t size) {
278 separator();
279 encodeBase64String((const unsigned char *)bytes, size);
280 value = true;
281 space = ' ';
282 }
283
José Fonseca589082d2011-03-30 09:10:40 +0100284 inline void writeNull(void) {
285 separator();
286 os << "null";
287 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100288 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100289 }
290
291 inline void writeBool(bool b) {
292 separator();
293 os << (b ? "true" : "false");
294 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100295 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100296 }
297
298 template<class T>
José Fonseca23a17d62011-04-09 12:22:58 +0100299 inline void writeNumber(T n) {
José Fonseca589082d2011-03-30 09:10:40 +0100300 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100301 os << std::dec << n;
José Fonseca589082d2011-03-30 09:10:40 +0100302 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100303 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100304 }
José Fonseca23a17d62011-04-09 12:22:58 +0100305
306 inline void writeStringMember(const char *name, const char *s) {
307 beginMember(name);
308 writeString(s);
309 endMember();
310 }
311
312 inline void writeBoolMember(const char *name, bool b) {
313 beginMember(name);
314 writeBool(b);
315 endMember();
316 }
317
318 template<class T>
319 inline void writeNumberMember(const char *name, T n) {
320 beginMember(name);
321 writeNumber(n);
322 endMember();
323 }
José Fonseca589082d2011-03-30 09:10:40 +0100324};
325
326#endif /* _JSON_HPP_ */