blob: 5af20c5278b053da560e4c09583d69a8297a2de3 [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é Fonseca589082d2011-03-30 09:10:40 +010069 }
70 }
71
José Fonseca702c1c62011-04-08 07:58:05 +010072 void escapeAsciiString(const char *str) {
José Fonseca589082d2011-03-30 09:10:40 +010073 os << "\"";
José Fonseca702c1c62011-04-08 07:58:05 +010074
75 const unsigned char *src = (const unsigned char *)str;
José Fonseca589082d2011-03-30 09:10:40 +010076 unsigned char c;
José Fonseca702c1c62011-04-08 07:58:05 +010077 while ((c = *src++)) {
78 if ((c == '\"') ||
79 (c == '\\')) {
80 // escape character
81 os << '\\' << (unsigned char)c;
82 } else if ((c >= 0x20 && c <= 0x7e) ||
83 c == '\t' ||
84 c == '\r' ||
85 c == '\n') {
86 // pass-through character
87 os << (unsigned char)c;
88 } else {
89 assert(0);
90 os << "?";
José Fonseca589082d2011-03-30 09:10:40 +010091 }
92 }
José Fonseca702c1c62011-04-08 07:58:05 +010093
94 os << "\"";
95 }
96
97 void escapeUnicodeString(const char *str) {
98 os << "\"";
99
100 const char *locale = setlocale(LC_CTYPE, "");
101 const char *src = str;
102 mbstate_t state;
103
104 memset(&state, 0, sizeof state);
105
106 do {
107 // Convert characters one at a time in order to recover from
108 // conversion errors
109 wchar_t c;
110 size_t written = mbsrtowcs(&c, &src, 1, &state);
111 if (written == 0) {
112 // completed
113 break;
114 } if (written == (size_t)-1) {
115 // conversion error -- skip
116 os << "?";
117 do {
118 ++src;
119 } while (*src & 0x80);
120 } else if ((c == '\"') ||
121 (c == '\\')) {
122 // escape character
123 os << '\\' << (unsigned char)c;
124 } else if ((c >= 0x20 && c <= 0x7e) ||
125 c == '\t' ||
126 c == '\r' ||
127 c == '\n') {
128 // pass-through character
129 os << (unsigned char)c;
130 } else {
131 // unicode
132 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
133 os << std::dec;
134 }
135 } while (src);
136
137 setlocale(LC_CTYPE, locale);
138
José Fonseca589082d2011-03-30 09:10:40 +0100139 os << "\"";
140 }
141
142public:
143 JSONWriter(std::ostream &_os) :
144 os(_os),
145 level(0),
José Fonsecafc92b762011-04-08 09:44:26 +0100146 value(false),
147 space(0)
José Fonseca589082d2011-03-30 09:10:40 +0100148 {
149 beginObject();
150 }
151
152 ~JSONWriter() {
153 endObject();
154 newline();
155 }
156
157 inline void beginObject() {
José Fonseca3b4677e2011-04-07 10:14:02 +0100158 separator();
José Fonseca589082d2011-03-30 09:10:40 +0100159 os << "{";
160 ++level;
161 value = false;
162 }
163
164 inline void endObject() {
165 --level;
166 if (value)
167 newline();
168 os << "}";
169 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100170 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100171 }
172
173 inline void beginMember(const char * name) {
José Fonsecafc92b762011-04-08 09:44:26 +0100174 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100175 separator();
176 newline();
José Fonseca702c1c62011-04-08 07:58:05 +0100177 escapeAsciiString(name);
José Fonseca589082d2011-03-30 09:10:40 +0100178 os << ": ";
179 value = false;
180 }
181
182 inline void endMember(void) {
183 assert(value);
184 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100185 space = 0;
José Fonseca589082d2011-03-30 09:10:40 +0100186 }
187
188 inline void beginArray() {
189 separator();
190 os << "[";
José Fonsecafc92b762011-04-08 09:44:26 +0100191 ++level;
José Fonseca589082d2011-03-30 09:10:40 +0100192 value = false;
193 }
194
195 inline void endArray(void) {
José Fonsecafc92b762011-04-08 09:44:26 +0100196 --level;
José Fonseca589082d2011-03-30 09:10:40 +0100197 os << "]";
198 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100199 space = '\n';
José Fonseca589082d2011-03-30 09:10:40 +0100200 }
201
202 inline void writeString(const char *s) {
203 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100204 escapeUnicodeString(s);
José Fonsecaed2167c2011-03-31 01:15:23 +0100205 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100206 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100207 }
208
209 inline void writeNull(void) {
210 separator();
211 os << "null";
212 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100213 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100214 }
215
216 inline void writeBool(bool b) {
217 separator();
218 os << (b ? "true" : "false");
219 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100220 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100221 }
222
223 template<class T>
224 void writeNumber(T n) {
225 separator();
José Fonseca702c1c62011-04-08 07:58:05 +0100226 os << std::dec << n;
José Fonseca589082d2011-03-30 09:10:40 +0100227 value = true;
José Fonsecafc92b762011-04-08 09:44:26 +0100228 space = ' ';
José Fonseca589082d2011-03-30 09:10:40 +0100229 }
230};
231
232#endif /* _JSON_HPP_ */