RapidJson Writer:没有正确处理UTF16编码的德语重音字符

时间:2018-04-22 10:38:31

标签: c++ json

我需要为包含德语口音字符的字符串值创建json Unicode文本:Prüfung

我实现了这段代码:

using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericStringBuffer<UTF16<> > StringBufferW;

StringBufferW sb;
Writer<StringBufferW, UTF16<>> writerUTF16(sb); 

std::wstring name = L"Prüfung";

writerUTF16.StartObject();
{
    writerUTF16.String(L"name");
    writerUTF16.String(name.c_str(), static_cast<SizeType>(name.length()));
}
writerUTF16.EndObject();

std::wstring jsonText = sb.GetString();

哪个不起作用,jsonText的输出是:{“name”:“Prㅐㅌfung”}

谁能告诉我我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

查看rapidjson Writer源码,定义为:

template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
class Writer

在您的编写器定义中,您仅将 UTF16 设置为 SourceEncoding,但保留默认为 UTF8 的 TargetEncoding。

请考虑更改:

Writer<StringBufferW, UTF16<>> writerUTF16(sb);

Writer<StringBufferW, UTF16<>, UTF16<>> writerUTF16(sb);