0

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

yary
  • 15
  • 1
  • 4

1 Answers1

3

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());
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks so much @Remy, just a note for other people with the same issue. Don't forget to call `writerUTF16.StartObject()` – yary Oct 28 '14 at 09:58
  • That depends on what you want the JSON to look like. `StartObject()` starts a new object, and thus writes an open `{` to the StringBuffer`. If you are just trying to write a UTF-16 string to an existing object, you don't want to start a new object. – Remy Lebeau Oct 28 '14 at 17:25
  • maybe I don't understand it fully. When I used the same `StringBuffer s` for writerUTF8 and writerUTF16 it was possible to compile it, but the application crashed (R6010 abort() has been called). I am not able to find what exactly this construct means `Writer< StringBuffer > writerUTF8(s);` Writer is a class, but what is the purpose of `<`? I guess that it is overloading, but never seen similar syntax and cant find a page with explanation. – yary Oct 29 '14 at 12:14