jsoncpp打包json格式toStyledString输出中文乱码解决方案
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
1.修改json_writer.cpp中valueToQuotedStringN函数
具体修改如下:
static String valueToQuotedStringN(const char* value, size_t length,
bool emitUTF8 = false) {
if (value == nullptr)
return "";
if (!doesAnyCharRequireEscaping(value, length))
return String("\"") + value + "\"";
// We have to walk value and escape any special characters.
// Appending to String is not efficient, but this should be rare.
// (Note: forward slashes are *not* rare, but I am not escaping them.)
String::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
String result;
result.reserve(maxsize); // to avoid lots of mallocs
result += "\"";
char const* end = value + length;
for (const char* c = value; c != end; ++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.)
// blep notes: actually escaping \/ may be useful in javascript to avoid </
// sequence.
// Should add a flag to allow this compatibility mode and prevent this
// sequence from occurring.
default:
//if (emitUTF8) {
// unsigned codepoint = static_cast<unsigned char>(*c);
// if (codepoint < 0x20) {
// appendHex(result, codepoint);
// } else {
// appendRaw(result, codepoint);
// }
//} else {
// unsigned codepoint = utf8ToCodepoint(c, end); // modifies `c`
// if (codepoint < 0x20) {
// appendHex(result, codepoint);
// } else if (codepoint < 0x80) {
// appendRaw(result, codepoint);
// } else if (codepoint < 0x10000) {
// // Basic Multilingual Plane
// appendHex(result, codepoint);
// } else {
// // Extended Unicode. Encode 20 bits as a surrogate pair.
// codepoint -= 0x10000;
// appendHex(result, 0xd800 + ((codepoint >> 10) & 0x3ff));
// appendHex(result, 0xdc00 + (codepoint & 0x3ff));
// }
//}
result += *c;
break;
}
}
result += "\"";
return result;
}
主要修改为default部分,注释掉原来的代码块,新加 result += *c;,改了之后保存,中文就不会乱码了。
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
2 个月前
8c391e04
5 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)