fix json unicode output

This commit is contained in:
TangoCash
2016-03-21 20:16:33 +01:00
committed by svenhoefer
parent 0461049621
commit 5dadb260c1
2 changed files with 50 additions and 0 deletions

View File

@@ -296,6 +296,53 @@ std::string json_out_error(std::string _error) {
// JSON: convert string to JSON-String // JSON: convert string to JSON-String
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::string json_convert_string(std::string value)
{
std::string result;
for (size_t i = 0; i < value.length(); i++)
{
unsigned char c = unsigned(value[i]);
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;
default:
if ( isControlCharacter( c ) )
{
std::ostringstream oss;
oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(c);
result += oss.str();
}
else
{
result += c;
}
break;
}
}
return result;
}
#if 0
std::string json_convert_string(std::string s) { std::string json_convert_string(std::string s) {
std::stringstream ss; std::stringstream ss;
for (size_t i = 0; i < s.length(); ) { for (size_t i = 0; i < s.length(); ) {
@@ -350,6 +397,7 @@ std::string json_convert_string(std::string s) {
} }
return ss.str(); return ss.str();
} }
#endif // 0
std::string yExecuteScript(std::string cmd) { std::string yExecuteScript(std::string cmd) {
std::string script, para, result; std::string script, para, result;

View File

@@ -48,6 +48,8 @@ std::string json_out_pair(std::string _key, std::string _value);
std::string json_out_success(std::string _result); std::string json_out_success(std::string _result);
std::string json_out_error(std::string _error); std::string json_out_error(std::string _error);
std::string json_convert_string(std::string s); std::string json_convert_string(std::string s);
/// Returns true if ch is a control character (in range [0,32]).
static inline bool isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F;}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Script Helpers // Script Helpers