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.
41 JSONWriter::newline(void) {
43 for (int i = 0; i < level; ++i)
48 JSONWriter::separator(void) {
69 escapeAsciiString(std::ostream &os, const char *str) {
72 const unsigned char *src = (const unsigned char *)str;
74 while ((c = *src++)) {
78 os << '\\' << (unsigned char)c;
79 } else if ((c >= 0x20 && c <= 0x7e) ||
83 // pass-through character
84 os << (unsigned char)c;
95 escapeUnicodeString(std::ostream &os, const char *str) {
98 const char *locale = setlocale(LC_CTYPE, "");
99 const char *src = str;
102 memset(&state, 0, sizeof state);
105 // Convert characters one at a time in order to recover from
108 size_t written = mbsrtowcs(&c, &src, 1, &state);
112 } if (written == (size_t)-1) {
113 // conversion error -- skip
117 } while (*src & 0x80);
118 } else if ((c == '\"') ||
121 os << '\\' << (unsigned char)c;
122 } else if ((c >= 0x20 && c <= 0x7e) ||
126 // pass-through character
127 os << (unsigned char)c;
130 os << "\\u" << std::setfill('0') << std::hex << std::setw(4) << (unsigned)c;
135 setlocale(LC_CTYPE, locale);
141 encodeBase64String(std::ostream &os, const unsigned char *bytes, size_t size) {
142 const char *table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
143 unsigned char c0, c1, c2, c3;
152 c1 = ((bytes[0] & 0x03) << 4) | ((bytes[1] & 0xf0) >> 4);
153 c2 = ((bytes[1] & 0x0f) << 2) | ((bytes[2] & 0xc0) >> 6);
154 c3 = bytes[2] & 0x3f;
156 buf[0] = table64[c0];
157 buf[1] = table64[c1];
158 buf[2] = table64[c2];
159 buf[3] = table64[c3];
167 if (written >= 76/4 && size) {
175 c1 = ((bytes[0] & 0x03) << 4);
180 c1 |= ((bytes[1] & 0xf0) >> 4);
181 c2 = ((bytes[1] & 0x0f) << 2);
183 c2 |= ((bytes[2] & 0xc0) >> 6);
184 c3 = bytes[2] & 0x3f;
185 buf[3] = table64[c3];
187 buf[2] = table64[c2];
189 buf[1] = table64[c1];
190 buf[0] = table64[c0];
198 JSONWriter::JSONWriter(std::ostream &_os) :
207 JSONWriter::~JSONWriter() {
213 JSONWriter::beginObject() {
221 JSONWriter::endObject() {
231 JSONWriter::beginMember(const char * name) {
235 escapeAsciiString(os, name);
241 JSONWriter::endMember(void) {
248 JSONWriter::beginArray() {
257 JSONWriter::endArray(void) {
268 JSONWriter::writeString(const char *s) {
275 escapeUnicodeString(os, s);
281 JSONWriter::writeBase64(const void *bytes, size_t size) {
283 encodeBase64String(os, (const unsigned char *)bytes, size);
289 JSONWriter::writeNull(void) {
297 JSONWriter::writeBool(bool b) {
299 os << (b ? "true" : "false");
305 JSONWriter::writeImage(image::Image *image, const char *format, unsigned depth)
314 // Tell the GUI this is no ordinary object, but an image
315 writeStringMember("__class__", "image");
317 writeIntMember("__width__", image->width);
318 writeIntMember("__height__", image->height / depth);
319 writeIntMember("__depth__", depth);
321 writeStringMember("__format__", format);
323 beginMember("__data__");
324 std::stringstream ss;
326 if (image->channelType == image::TYPE_UNORM8) {
332 const std::string & s = ss.str();
333 writeBase64(s.data(), s.size());
334 endMember(); // __data__