| 4256 | JSONCPP_STRING valueToString(bool value) { return value ? "true" : "false"; } |
| 4257 | |
| 4258 | JSONCPP_STRING valueToQuotedString(const char* value) { |
| 4259 | if (value == NULL) |
| 4260 | return ""; |
| 4261 | // Not sure how to handle unicode... |
| 4262 | if (strpbrk(value, "\"\\\b\f\n\r\t") == NULL && |
| 4263 | !containsControlCharacter(value)) |
| 4264 | return JSONCPP_STRING("\"") + value + "\""; |
| 4265 | // We have to walk value and escape any special characters. |
| 4266 | // Appending to JSONCPP_STRING is not efficient, but this should be rare. |
| 4267 | // (Note: forward slashes are *not* rare, but I am not escaping them.) |
| 4268 | JSONCPP_STRING::size_type maxsize = |
| 4269 | strlen(value) * 2 + 3; // allescaped+quotes+NULL |
| 4270 | JSONCPP_STRING result; |
| 4271 | result.reserve(maxsize); // to avoid lots of mallocs |
| 4272 | result += "\""; |
| 4273 | for (const char* c = value; *c != 0; ++c) { |
| 4274 | switch (*c) { |
| 4275 | case '\"': |
| 4276 | result += "\\\""; |
| 4277 | break; |
| 4278 | case '\\': |
| 4279 | result += "\\\\"; |
| 4280 | break; |
| 4281 | case '\b': |
| 4282 | result += "\\b"; |
| 4283 | break; |
| 4284 | case '\f': |
| 4285 | result += "\\f"; |
| 4286 | break; |
| 4287 | case '\n': |
| 4288 | result += "\\n"; |
| 4289 | break; |
| 4290 | case '\r': |
| 4291 | result += "\\r"; |
| 4292 | break; |
| 4293 | case '\t': |
| 4294 | result += "\\t"; |
| 4295 | break; |
| 4296 | // case '/': |
| 4297 | // Even though \/ is considered a legal escape in JSON, a bare |
| 4298 | // slash is also legal, so I see no reason to escape it. |
| 4299 | // (I hope I am not misunderstanding something. |
| 4300 | // blep notes: actually escaping \/ may be useful in javascript to avoid </ |
| 4301 | // sequence. |
| 4302 | // Should add a flag to allow this compatibility mode and prevent this |
| 4303 | // sequence from occurring. |
| 4304 | default: |
| 4305 | if (isControlCharacter(*c)) { |
| 4306 | JSONCPP_OSTRINGSTREAM oss; |
| 4307 | oss << "\\u" << std::hex << std::uppercase << std::setfill('0') |
| 4308 | << std::setw(4) << static_cast<int>(*c); |
| 4309 | result += oss.str(); |
| 4310 | } else { |
| 4311 | result += *c; |
| 4312 | } |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
no test coverage detected