RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* json_printing.cc 00002 Jeremy Barnes, 8 March 2013 00003 Copyright (c) 2013 Datacratic Inc. All rights reserved. 00004 00005 Functionality to print JSON values. 00006 */ 00007 00008 #include "json_printing.h" 00009 00010 00011 using namespace std; 00012 00013 00014 namespace Datacratic { 00015 00016 00017 void 00018 StreamJsonPrintingContext:: 00019 writeStringUtf8(const Utf8String & s) 00020 { 00021 stream << '\"'; 00022 00023 for (auto it = s.begin(), end = s.end(); it != end; ++it) { 00024 int c = *it; 00025 if (c >= ' ' && c < 127 && c != '\"' && c != '\\') 00026 stream << (char)c; 00027 else { 00028 switch (c) { 00029 case '\t': stream << "\\t"; break; 00030 case '\n': stream << "\\n"; break; 00031 case '\r': stream << "\\r"; break; 00032 case '\b': stream << "\\b"; break; 00033 case '\f': stream << "\\f"; break; 00034 case '/': 00035 case '\\': 00036 case '\"': stream << '\\' << (char)c; break; 00037 default: 00038 if (writeUtf8) { 00039 char buf[4]; 00040 char * p = utf8::unchecked::append(c, buf); 00041 stream.write(buf, p - buf); 00042 } 00043 else { 00044 ExcAssert(c >= 0 && c < 65536); 00045 stream << ML::format("\\u%04x", (unsigned)c); 00046 } 00047 } 00048 } 00049 } 00050 00051 stream << '\"'; 00052 } 00053 00054 00055 } // namespace Datacratic