Rapidjson根本不编码utf8序列 [英] Rapidjson does not encode utf8 sequence at all
问题描述
我正在尝试使用Rapidjson将utf8序列转义为\uXXXX
格式,但是不起作用.
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.
我尝试使用AutoUTF模板,但是这里也没有用于内存流的文档
I tried to use AutoUTF template, but here's no document for memory stream either
有什么想法吗?我也尝试过jsoncpp,但是该库也不支持Unicode
Any ideas? I tried jsoncpp as well, but that library does not support unicode as well
感谢@Milo Yip,我忘了提到我正在使用Visual Studio 2010
Thanks @Milo Yip, I forget to mention I'm using Visual Studio 2010
推荐答案
我尝试在OS X上运行,并且有效:
I tried on OS X and it works:
{"chinese":"\u4E2D\u6587\u6D4B\u8BD5"}
我认为问题在于,您使用的编译器未将文字字符串"中文测试"
编码为UTF-8. Linux/OSX将源代码视为UTF-8,但Windows默认情况下不会.
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.
您可以尝试使用C ++ 11 UTF-8文字u8"中文测试"
.或者,您可以从UTF-8编码的文件中读取字符串以进行测试.
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.
问题已更新.在Visual Studio 2010及更高版本上,有一个未记录的功能:
The question was updated. On Visual Studio 2010 and after, there is an undocumented feature:
#pragma execution_character_set("utf-8")
诸如此类,文字字符以UTF-8编码.对于兼容C ++ 11的编译器,应使用u8"xxx"
文字.
Such that the literal characters are encoded in UTF-8. For C++11 compatible compilers, u8"xxx"
literal should be used.
无论如何,"Rapidjson根本不编码utf8序列"的说法是错误的.
Anyway, the claim that "Rapidjson does not encode utf8 sequence at all" is incorrect.
这篇关于Rapidjson根本不编码utf8序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!