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.
54 for (int i = 0; i < level; ++i)
58 void separator(void) {
78 void escapeAsciiString(const char *str) {
81 const unsigned char *src = (const unsigned char *)str;
83 while ((c = *src++)) {
87 os << '\\' << (unsigned char)c;
88 } else if ((c >= 0x20 && c <= 0x7e) ||
92 // pass-through character
93 os << (unsigned char)c;
103 void escapeUnicodeString(const char *str) {
106 const char *locale = setlocale(LC_CTYPE, "");
107 const char *src = str;
110 memset(&state, 0, sizeof state);
113 // Convert characters one at a time in order to recover from
116 size_t written = mbsrtowcs(&c, &src, 1, &state);
120 } if (written == (size_t)-1) {
121 // conversion error -- skip
125 } while (*src & 0x80);
126 } else if ((c == '\"') ||
129 os << '\\' << (unsigned char)c;
130 } else if ((c >= 0x20 && c <= 0x7e) ||
134 // pass-through character
135 os << (unsigned char)c;
138 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
143 setlocale(LC_CTYPE, locale);
148 void encodeBase64String(const unsigned char *bytes, size_t size) {
149 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
150 unsigned char c0, c1, c2, c3;
159 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
160 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
161 c3 = bytes[2] & 0x3f;
163 buf[0] = table64[c0];
164 buf[1] = table64[c1];
165 buf[2] = table64[c2];
166 buf[3] = table64[c3];
174 if (written >= 76/4 && size) {
182 c1 = ((bytes[0] & 0x03) << 4);
187 c1 |= ((bytes[1] & 0xf0) >> 4);
188 c2 = ((bytes[1] & 0x0f) << 2);
190 c2 |= ((bytes[2] & 0xc0) >> 6);
191 c3 = bytes[2] & 0x3f;
192 buf[3] = table64[c3];
194 buf[2] = table64[c2];
196 buf[1] = table64[c1];
197 buf[0] = table64[c0];
206 JSONWriter(std::ostream &_os) :
220 inline void beginObject() {
227 inline void endObject() {
236 inline void beginMember(const char * name) {
240 escapeAsciiString(name);
245 inline void beginMember(const std::string &name) {
246 beginMember(name.c_str());
249 inline void endMember(void) {
255 inline void beginArray() {
263 inline void endArray(void) {
273 inline void writeString(const char *s) {
280 escapeUnicodeString(s);
285 inline void writeString(const std::string &s) {
286 writeString(s.c_str());
289 inline void writeBase64(const void *bytes, size_t size) {
291 encodeBase64String((const unsigned char *)bytes, size);
296 inline void writeNull(void) {
303 inline void writeBool(bool b) {
305 os << (b ? "true" : "false");
312 * Special case for char to prevent it to be written as a literal
315 inline void writeNumber(char n) {
317 os << std::dec << static_cast<int>(n);
322 inline void writeNumber(unsigned char n) {
324 os << std::dec << static_cast<unsigned>(n);
330 inline void writeNumber(T n) {
336 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
342 inline void writeStringMember(const char *name, const char *s) {
348 inline void writeBoolMember(const char *name, bool b) {
355 inline void writeNumberMember(const char *name, T n) {
362 #endif /* _JSON_HPP_ */