blob: 7326891fd1f6939d4044b259f61e1ffe314d4687 [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
146public:
147 JSONWriter(std::ostream &_os) :
148 os(_os),
149 level(0),
José Fonsecafc92b762011-04-08 09:44:26 +0100150 value(false),
151 space(0)
José Fonseca589082d2011-03-30 09:10:40 +0100152 {
153 beginObject();
154 }
155
156 ~JSONWriter() {
157 endObject();
158 newline();
159 }
160
161 inline void beginObject() {
José Fonseca3b4677e2011-04-07 10:14:02 +0100162 separator();
José Fonseca589082d2011-03-30 09:10:40 +0100163 os << "{";
164 ++level;
165 value = false;
166 }
167
168 inline void endObject() {
169 --level;
170 if (value)
171 newline();
172 os << "}";
173 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100174 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100175 }
176
177 inline void beginMember(const char * name) {
José Fonsecafc92b762011-04-08 09:44:26 +0100178 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100179 separator();
180 newline();
José Fonseca702c1c62011-04-08 07:58:05 +0100181 escapeAsciiString(name);
José Fonseca589082d2011-03-30 09:10:40 +0100182 os << ": ";
183 value = false;
184 }
185
186 inline void endMember(void) {
187 assert(value);
188 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100189 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100190 }
191
192 inline void beginArray() {
193 separator();
194 os << "[";
José Fonsecafc92b762011-04-08 09:44:26 +0100195 ++level;
José Fonseca589082d2011-03-30 09:10:40 +0100196 value = false;
José Fonseca2e8febd2011-04-09 00:10:30 +0100197 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100198 }
199
200 inline void endArray(void) {
José Fonsecafc92b762011-04-08 09:44:26 +0100201 --level;
José Fonseca2e8febd2011-04-09 00:10:30 +0100202 if (space == '\n') {
203 newline();
204 }
José Fonseca589082d2011-03-30 09:10:40 +0100205 os << "]";
206 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100207 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100208 }
209
210 inline void writeString(const char *s) {
211 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100212 escapeUnicodeString(s);
José Fonsecaed2167c2011-03-31 01:15:23 +0100213 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100214 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100215 }
216
217 inline void writeNull(void) {
218 separator();
219 os << "null";
220 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100221 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100222 }
223
224 inline void writeBool(bool b) {
225 separator();
226 os << (b ? "true" : "false");
227 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100228 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100229 }
230
231 template<class T>
232 void writeNumber(T n) {
233 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100234 os << std::dec << n;
José Fonseca589082d2011-03-30 09:10:40 +0100235 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100236 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100237 }
238};
239
240#endif /* _JSON_HPP_ */