使用jsoncpp写中文乱码的坑

1. 问题描述
当使用老版本jsoncpp写中文,使用新版本的读取,就会乱码。

2. jsoncpp 源码地址:
https://github.com/open-source-parsers/jsoncpp.git

3. 字节流类型
string-escape 对二进制的字节流,
unicode-escape unicode码点值流

4. GitHub上的提交记录
Revision: 42a161fc80b32cc63c1a3e7ab1c9ed588a83edaa
Author: Paweł Kierski pkierski@gmail.com
Date: 2017/10/4 9:19:20
Message:
Serialize UTF-8 string with Unicode escapes (#687)
Squashed and merged.

Modified: src/lib_json/json_writer.cpp

5. 代码对比
2017/10/4 以前的版本
序列化字符串使用的utf8字符串值的16进制表示 (string-escape)

std::string valueToQuotedString( const char *value ){

switch(*c){
default:\不是特殊字符,16进制表示
if ( isControlCharacter( *c ) ){
std::ostringstream oss;
oss << “\u” << std::hex << std::uppercase << std::setfill(‘0’) << std::setw(4) << static_cast(*c);
result += oss.str();
}
}
}

bool isControlCharacter(char ch){
return ch > 0 && ch <= 0x1F;
}

2017/10/4后的版本
使用utf8字符串 码点值的16进制表示(unicode-escape)

static JSONCPP_STRING valueToQuotedStringN(const char* value, unsigned length) {
//
//转化到码点上,我的理解就是unicode的码表值
unsigned int cp = utf8ToCodepoint(c, end);
// don’t escape non-control characters
// (short escape sequence are applied above)
if (cp < 0x80 && cp >= 0x20)
result += static_cast(cp);
else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
result += “\u”;
result += toHex16Bit(cp);
} else { // codepoint is not in Basic Multilingual Plane
// convert to surrogate pair first
cp -= 0x10000;
result += “\u”;
result += toHex16Bit((cp >> 10) + 0xD800);
result += “\u”;
result += toHex16Bit((cp & 0x3FF) + 0xDC00);
}
}

6. 切记,不要随便升级库,当与服务端或其他端调试通的情况下,不要随便升级。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值