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.
53 for (int i = 0; i < level; ++i)
57 void separator(void) {
77 void escapeAsciiString(const char *str) {
80 const unsigned char *src = (const unsigned char *)str;
82 while ((c = *src++)) {
86 os << '\\' << (unsigned char)c;
87 } else if ((c >= 0x20 && c <= 0x7e) ||
91 // pass-through character
92 os << (unsigned char)c;
102 void escapeUnicodeString(const char *str) {
105 const char *locale = setlocale(LC_CTYPE, "");
106 const char *src = str;
109 memset(&state, 0, sizeof state);
112 // Convert characters one at a time in order to recover from
115 size_t written = mbsrtowcs(&c, &src, 1, &state);
119 } if (written == (size_t)-1) {
120 // conversion error -- skip
124 } while (*src & 0x80);
125 } else if ((c == '\"') ||
128 os << '\\' << (unsigned char)c;
129 } else if ((c >= 0x20 && c <= 0x7e) ||
133 // pass-through character
134 os << (unsigned char)c;
137 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
142 setlocale(LC_CTYPE, locale);
147 void encodeBase64String(const unsigned char *bytes, size_t size) {
148 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
149 unsigned char c0, c1, c2, c3;
158 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
159 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
160 c3 = bytes[2] & 0x3f;
162 buf[0] = table64[c0];
163 buf[1] = table64[c1];
164 buf[2] = table64[c2];
165 buf[3] = table64[c3];
173 if (written >= 76/4 && size) {
181 c1 = ((bytes[0] & 0x03) << 4);
186 c1 |= ((bytes[1] & 0xf0) >> 4);
187 c2 = ((bytes[1] & 0x0f) << 2);
189 c2 |= ((bytes[2] & 0xc0) >> 6);
190 c3 = bytes[2] & 0x3f;
191 buf[3] = table64[c3];
193 buf[2] = table64[c2];
195 buf[1] = table64[c1];
196 buf[0] = table64[c0];
205 JSONWriter(std::ostream &_os) :
219 inline void beginObject() {
226 inline void endObject() {
235 inline void beginMember(const char * name) {
239 escapeAsciiString(name);
244 inline void beginMember(const std::string &name) {
245 beginMember(name.c_str());
248 inline void endMember(void) {
254 inline void beginArray() {
262 inline void endArray(void) {
272 inline void writeString(const char *s) {
279 escapeUnicodeString(s);
284 inline void writeString(const std::string &s) {
285 writeString(s.c_str());
288 inline void writeBase64(const void *bytes, size_t size) {
290 encodeBase64String((const unsigned char *)bytes, size);
295 inline void writeNull(void) {
302 inline void writeBool(bool b) {
304 os << (b ? "true" : "false");
310 inline void writeNumber(T n) {
316 os << std::dec << std::setprecision(9) << n;
322 inline void writeStringMember(const char *name, const char *s) {
328 inline void writeBoolMember(const char *name, bool b) {
335 inline void writeNumberMember(const char *name, T n) {
342 #endif /* _JSON_HPP_ */