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 * JSON writing functions.
38 JSONWriter::newline(void) {
40 for (int i = 0; i < level; ++i)
45 JSONWriter::separator(void) {
66 escapeAsciiString(std::ostream &os, const char *str) {
69 const unsigned char *src = (const unsigned char *)str;
71 while ((c = *src++)) {
75 os << '\\' << (unsigned char)c;
76 } else if ((c >= 0x20 && c <= 0x7e) ||
80 // pass-through character
81 os << (unsigned char)c;
92 escapeUnicodeString(std::ostream &os, const char *str) {
95 const char *locale = setlocale(LC_CTYPE, "");
96 const char *src = str;
99 memset(&state, 0, sizeof state);
102 // Convert characters one at a time in order to recover from
105 size_t written = mbsrtowcs(&c, &src, 1, &state);
109 } if (written == (size_t)-1) {
110 // conversion error -- skip
114 } while (*src & 0x80);
115 } else if ((c == '\"') ||
118 os << '\\' << (unsigned char)c;
119 } else if ((c >= 0x20 && c <= 0x7e) ||
123 // pass-through character
124 os << (unsigned char)c;
127 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
132 setlocale(LC_CTYPE, locale);
138 encodeBase64String(std::ostream &os, const unsigned char *bytes, size_t size) {
139 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140 unsigned char c0, c1, c2, c3;
149 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
150 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
151 c3 = bytes[2] & 0x3f;
153 buf[0] = table64[c0];
154 buf[1] = table64[c1];
155 buf[2] = table64[c2];
156 buf[3] = table64[c3];
164 if (written >= 76/4 && size) {
172 c1 = ((bytes[0] & 0x03) << 4);
177 c1 |= ((bytes[1] & 0xf0) >> 4);
178 c2 = ((bytes[1] & 0x0f) << 2);
180 c2 |= ((bytes[2] & 0xc0) >> 6);
181 c3 = bytes[2] & 0x3f;
182 buf[3] = table64[c3];
184 buf[2] = table64[c2];
186 buf[1] = table64[c1];
187 buf[0] = table64[c0];
195 JSONWriter::JSONWriter(std::ostream &_os) :
204 JSONWriter::~JSONWriter() {
210 JSONWriter::beginObject() {
218 JSONWriter::endObject() {
228 JSONWriter::beginMember(const char * name) {
232 escapeAsciiString(os, name);
238 JSONWriter::endMember(void) {
245 JSONWriter::beginArray() {
254 JSONWriter::endArray(void) {
265 JSONWriter::writeString(const char *s) {
272 escapeUnicodeString(os, s);
278 JSONWriter::writeBase64(const void *bytes, size_t size) {
280 encodeBase64String(os, (const unsigned char *)bytes, size);
286 JSONWriter::writeNull(void) {
294 JSONWriter::writeBool(bool b) {
296 os << (b ? "true" : "false");