I came across a problem I am not sure how to solve. Here is my code.
#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());
}
When the project character set value is "Use Unicode Character Set", I am not able to compile. Only when it is set to "Use Multi-Byte Character Set".
Error says:
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>
I got a bad feeling that rapidjson supports only utf8 and it wont work with my current project which is all in utf16, or is there a way how to use it when character set is set to unicode?
Thank you
yary
If you look at the various string methods of the Writer
class, they all expect const SourceEncoding::Ch*
as input. By default, SourceEncoding
is rapidjson::UTF8
and Ch
is char
. To accept wchar_t*
input, you have to specify rapidjson::UTF16
(or rapidjson::UTF16LE
) as the SourceEncoding
, eg:
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());
}