VisualStudio 2013中的rapidJSON和UNICODE

时间:2014-10-25 13:19:15

标签: c++ unicode visual-studio-2013 rapidjson

我遇到了一个我不确定如何解决的问题。这是我的代码。

#include "rapidjson/write.h"
#include "rapidjson/stringbuffer.h"

...

void WriteResultToJSON()
{
  CHAR a[] = "a";
  TCHAR b[] = _T("b");
  WCHAR c[] = L"c";

  StringBuffer s;
  Writer<StringBuffer> writer(s);
  writer.StartObject();
  writer.String("A:");
  writer.String(a);
  writer.String("B");
  writer.String(b);
  writer.String("C");
  writer.String(c);
  write.EndObject();
  printf(s.GetString());
}

当项目字符集值为“使用Unicode字符集”时,我无法编译。仅当它设置为“使用多字节字符集”时。

错误说:

no instance of overloaded function "rapidjson::Writter<OutputStream, SourceEncoding, Target Encoding, StackAllocator>::String [with OutputStream=rapidjson::StringBuffer, SourceEncoding=rapidjson::UTF8<char>,TargetEncoding=rapidjson::UTF8<char>, StackAllocator=rapidjson::CtrAllocator]" matches the argument list
 argument types are (TCHAR  [2])
 object type is: rapidjson::Writter<rapidjson::StringBuffer, rapidjson::UTF8<char>, rapidjson::UTF8<char>, rapidjson::CrtAllocator>

我感觉不到rapidjson只支持utf8而且它不能用于我当前的项目,这个项目都是在utf16中,或者在字符集设置为unicode时有没有办法使用它?

谢谢

yary

1 个答案:

答案 0 :(得分:3)

如果查看Writer类的各种字符串方法,它们都希望const SourceEncoding::Ch*作为输入。默认情况下,SourceEncodingrapidjson::UTF8Chchar。要接受wchar_t*输入,您必须将rapidjson::UTF16(或rapidjson::UTF16LE)指定为SourceEncoding,例如:

void WriteResultToJSON()
{
    CHAR a[] = "a";
    TCHAR b[] = TEXT("b");
    WCHAR c[] = L"c";

    StringBuffer s;
    Writer< StringBuffer > writerUTF8(s); // UTF-8 input
    Writer< StringBuffer, UTF16<> > writerUTF16(s); // UTF-16 input

    writerUTF8.StartObject();
    writerUTF8.String("A:");
    writerUTF8.String(a);
    writerUTF8.String("B");

    #ifdef UNICODE
    writerUTF16.String(b);
    #else
    writerUTF8.String(b);
    #endif

    writerUTF8.String("C");
    writerUTF16.String(c);

    writerUTF8.EndObject();
    printf(s.GetString());
}