1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
27 * Trace writing functions.
39 # define isfinite _finite
42 # include <math.h> // isfinite, isnan
62 for (int i = 0; i < level; ++i)
66 void separator(void) {
86 void escapeAsciiString(const char *str) {
89 const unsigned char *src = (const unsigned char *)str;
91 while ((c = *src++)) {
95 os << '\\' << (unsigned char)c;
96 } else if ((c >= 0x20 && c <= 0x7e) ||
100 // pass-through character
101 os << (unsigned char)c;
111 void escapeUnicodeString(const char *str) {
114 const char *locale = setlocale(LC_CTYPE, "");
115 const char *src = str;
118 memset(&state, 0, sizeof state);
121 // Convert characters one at a time in order to recover from
124 size_t written = mbsrtowcs(&c, &src, 1, &state);
128 } if (written == (size_t)-1) {
129 // conversion error -- skip
133 } while (*src & 0x80);
134 } else if ((c == '\"') ||
137 os << '\\' << (unsigned char)c;
138 } else if ((c >= 0x20 && c <= 0x7e) ||
142 // pass-through character
143 os << (unsigned char)c;
146 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
151 setlocale(LC_CTYPE, locale);
156 void encodeBase64String(const unsigned char *bytes, size_t size) {
157 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
158 unsigned char c0, c1, c2, c3;
167 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
168 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
169 c3 = bytes[2] & 0x3f;
171 buf[0] = table64[c0];
172 buf[1] = table64[c1];
173 buf[2] = table64[c2];
174 buf[3] = table64[c3];
182 if (written >= 76/4 && size) {
190 c1 = ((bytes[0] & 0x03) << 4);
195 c1 |= ((bytes[1] & 0xf0) >> 4);
196 c2 = ((bytes[1] & 0x0f) << 2);
198 c2 |= ((bytes[2] & 0xc0) >> 6);
199 c3 = bytes[2] & 0x3f;
200 buf[3] = table64[c3];
202 buf[2] = table64[c2];
204 buf[1] = table64[c1];
205 buf[0] = table64[c0];
214 JSONWriter(std::ostream &_os) :
228 inline void beginObject() {
235 inline void endObject() {
244 inline void beginMember(const char * name) {
248 escapeAsciiString(name);
253 inline void beginMember(const std::string &name) {
254 beginMember(name.c_str());
257 inline void endMember(void) {
263 inline void beginArray() {
271 inline void endArray(void) {
281 inline void writeString(const char *s) {
288 escapeUnicodeString(s);
293 inline void writeString(const std::string &s) {
294 writeString(s.c_str());
297 inline void writeBase64(const void *bytes, size_t size) {
299 encodeBase64String((const unsigned char *)bytes, size);
304 inline void writeNull(void) {
311 inline void writeBool(bool b) {
313 os << (b ? "true" : "false");
320 * Special case for char to prevent it to be written as a literal
323 inline void writeInt(signed char n) {
325 os << std::dec << static_cast<int>(n);
330 inline void writeInt(unsigned char n) {
332 os << std::dec << static_cast<unsigned>(n);
338 inline void writeInt(T n) {
345 inline void writeFloat(T n) {
348 // NaN is non-standard but widely supported
350 } else if (!isfinite(n)) {
351 // Infinite is non-standard but widely supported
357 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
363 inline void writeStringMember(const char *name, const char *s) {
369 inline void writeBoolMember(const char *name, bool b) {
376 inline void writeIntMember(const char *name, T n) {
383 #endif /* _JSON_HPP_ */