Rapidjson does not encode utf8 sequence at all

我怕爱的太早我们不能终老 提交于 2019-12-11 13:06:27

问题


I'm trying to use rapidjson to escape utf8 sequences to \uXXXX format, but it's not working.

StringBuffer s;
Writer<StringBuffer, Document::EncodingType, ASCII<> > writer(s);
writer.StartObject();
writer.String("chinese");
writer.String("中文测试");
writer.EndObject();
cout << s.GetString() << endl;

The document says it would be escaped but actually it's all erased.

I tried to use AutoUTF template, but here's no document for memory stream either

Any ideas? I tried jsoncpp as well, but that library does not support unicode as well

Thanks @Milo Yip, I forget to mention I'm using Visual Studio 2010


回答1:


I tried on OS X and it works:

{"chinese":"\u4E2D\u6587\u6D4B\u8BD5"}

I think the problem is that, the compiler you are using does not encode the literal string "中文测试" into UTF-8. Linux/OSX treats source code as UTF-8 but Windows does not by default.

You can try to use C++11 UTF-8 literal u8"中文测试". Or you can read the strings from a UTF-8 encoded file for testing.


The question was updated. On Visual Studio 2010 and after, there is an undocumented feature:

#pragma execution_character_set("utf-8")

Such that the literal characters are encoded in UTF-8. For C++11 compatible compilers, u8"xxx" literal should be used.

Anyway, the claim that "Rapidjson does not encode utf8 sequence at all" is incorrect.



来源:https://stackoverflow.com/questions/29860029/rapidjson-does-not-encode-utf8-sequence-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!