diff --git a/src/lib_json/json_reader.cpp b/src/lib_json/json_reader.cpp index 2a4180d..fa3c5eb 100644 --- a/src/lib_json/json_reader.cpp +++ b/src/lib_json/json_reader.cpp @@ -560,6 +560,7 @@ Reader::decodeString( Token &token, std::string &decoded ) if ( !decodeUnicodeEscapeSequence( token, current, end, unicode ) ) return false; // @todo encode unicode as utf8. + // @todo remember to alter the writer too. } break; default: diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 47577fa..6fa5993 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -64,7 +64,45 @@ std::string valueToString( bool value ) std::string valueToQuotedString( const char *value ) { - return std::string("\"") + value + "\""; + // Not sure how to handle unicode... + if (std::strpbrk(value, "\"\\\b\f\n\r\t") == NULL) + return std::string("\"") + value + "\""; + // We have to walk value and escape any special characters. + // Appending to std::string is not efficient, but this should be rare. + // (Note: forward slashes are *not* rare, but I am not escaping them.) + std::string result("\""); + for (const char* c=value; *c != 0; ++c){ + switch(*c){ + case '\"': + result += "\\\""; + break; + case '\\': + result += "\\\\"; + break; + case '\b': + result += "\\b"; + break; + case '\f': + result += "\\f"; + break; + case '\n': + result += "\\n"; + break; + case '\r': + result += "\\r"; + break; + case '\t': + result += "\\t"; + break; + case '/': + // Even though \/ is considered a legal escape in JSON, a bare + // slash is also legal, so I see no reason to escape it. + // (I hope I am not misunderstanding something.) + default: + result += *c; + } + } + return result + "\""; } // Class Writer