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
41 # include <math.h> // isfinite
61 for (int i = 0; i < level; ++i)
65 void separator(void) {
85 void escapeAsciiString(const char *str) {
88 const unsigned char *src = (const unsigned char *)str;
90 while ((c = *src++)) {
94 os << '\\' << (unsigned char)c;
95 } else if ((c >= 0x20 && c <= 0x7e) ||
99 // pass-through character
100 os << (unsigned char)c;
110 void escapeUnicodeString(const char *str) {
113 const char *locale = setlocale(LC_CTYPE, "");
114 const char *src = str;
117 memset(&state, 0, sizeof state);
120 // Convert characters one at a time in order to recover from
123 size_t written = mbsrtowcs(&c, &src, 1, &state);
127 } if (written == (size_t)-1) {
128 // conversion error -- skip
132 } while (*src & 0x80);
133 } else if ((c == '\"') ||
136 os << '\\' << (unsigned char)c;
137 } else if ((c >= 0x20 && c <= 0x7e) ||
141 // pass-through character
142 os << (unsigned char)c;
145 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
150 setlocale(LC_CTYPE, locale);
155 void encodeBase64String(const unsigned char *bytes, size_t size) {
156 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
157 unsigned char c0, c1, c2, c3;
166 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
167 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
168 c3 = bytes[2] & 0x3f;
170 buf[0] = table64[c0];
171 buf[1] = table64[c1];
172 buf[2] = table64[c2];
173 buf[3] = table64[c3];
181 if (written >= 76/4 && size) {
189 c1 = ((bytes[0] & 0x03) << 4);
194 c1 |= ((bytes[1] & 0xf0) >> 4);
195 c2 = ((bytes[1] & 0x0f) << 2);
197 c2 |= ((bytes[2] & 0xc0) >> 6);
198 c3 = bytes[2] & 0x3f;
199 buf[3] = table64[c3];
201 buf[2] = table64[c2];
203 buf[1] = table64[c1];
204 buf[0] = table64[c0];
213 JSONWriter(std::ostream &_os) :
227 inline void beginObject() {
234 inline void endObject() {
243 inline void beginMember(const char * name) {
247 escapeAsciiString(name);
252 inline void beginMember(const std::string &name) {
253 beginMember(name.c_str());
256 inline void endMember(void) {
262 inline void beginArray() {
270 inline void endArray(void) {
280 inline void writeString(const char *s) {
287 escapeUnicodeString(s);
292 inline void writeString(const std::string &s) {
293 writeString(s.c_str());
296 inline void writeBase64(const void *bytes, size_t size) {
298 encodeBase64String((const unsigned char *)bytes, size);
303 inline void writeNull(void) {
310 inline void writeBool(bool b) {
312 os << (b ? "true" : "false");
319 * Special case for char to prevent it to be written as a literal
322 inline void writeNumber(char n) {
324 os << std::dec << static_cast<int>(n);
329 inline void writeNumber(unsigned char n) {
331 os << std::dec << static_cast<unsigned>(n);
337 inline void writeNumber(T n) {
343 os << std::dec << std::setprecision(std::numeric_limits<T>::digits10 + 1) << n;
349 inline void writeStringMember(const char *name, const char *s) {
355 inline void writeBoolMember(const char *name, bool b) {
362 inline void writeNumberMember(const char *name, T n) {
369 #endif /* _JSON_HPP_ */