1

I generated the following JSON using C++:

{
    "ProfileID": "DUO1",
    "ProfileName": "Sample"
}

The code used for generating that is:

string jsonData = "";
StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb);

writer.StartObject();
writer.Key("ProfileID");
writer.String(strProfileId.c_str());

writer.Key("ProfileName");                  
writer.String(strName.c_str());

writer.EndObject();
jsonData = sb.GetString();

It is working fine for ProfileName in English. If the profile name is in another language (say Chinese), how can this be done?

I want generate JSON like this:

{
    "ProfileID": "DUO1",
    "ProfileName": "不用客气"
}

I was able to set this Chinese name in wstring.

How to write this correctly to JSON?

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40

2 Answers2

0

RapidJSON supports UTF-8 by default. I am guessing the problem is with your compiler rather than with RapidJson. On linux, gcc treats source code as UTF-8, but on Windows MSVC does not.

On VS2008, you can change this on Tools > Options > Environment > Documents and checking Save documents as Unicode when data cannot be saved in codepage. On VS2010 and later, you can specify the encoding on a per-file basis with the pragma #pragma execution_character_set("utf-8").

You can then write things such as writer.String(u8"不用客气");.

Alberto Santini
  • 6,425
  • 1
  • 26
  • 37
  • thanks. I will try this option. This code should work in both Windows and MAC. For windows VS 2017 IDE and for MAC xCode IDE we are using – preethi selvaraju Jun 17 '18 at 09:03
  • I've tried the above solution. writer.String((u8)strName.c_str()); it is showing u8 undefined. If i use writer.String((u8)"不用客气"). It is storing in ascii only. – preethi selvaraju Jun 17 '18 at 10:34
  • If i use, writer.String((u8)"不用客气") i am getting the below: { "outputprofileid": "ICCFDUP1", "colorprofilename": "ä¸ç”¨å®¢æ°”" } – preethi selvaraju Jun 17 '18 at 10:44
  • @preethiselvaraju: What editor do you use for viewing JSON file? Notepad++? Visual Studio? This may be just an editor decoding problem. – sɐunıɔןɐqɐp Jun 17 '18 at 12:05
  • Am using Visual Studio 2017 IDE.If i use writer.String(u8"不用客气"), it is working fine. But how to code by passing variable name. strProfileName has the chinese name. writer.String(strProfileName). If i use writer.String((u8)strProfileName.c_str()) it is throwing error. – preethi selvaraju Jun 18 '18 at 04:14
  • It seems you think `u8` is a [cast operator](http://en.cppreference.com/w/cpp/language/cast_operator), but it actually is a [string literal prefix](http://en.cppreference.com/w/cpp/language/string_literal). How you get UTF-8 into a `string` (or `wstring`) is another question, however, and [can be platform-dependent](https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring). It is not really related to RapidJSON. – Alberto Santini Jun 18 '18 at 09:24
0

This is the signature for PrettyWritter.

template<typename Stream, typename Encoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
class PrettyWriter : public Writer<Stream, Encoding, Allocator>
{...}

It takes Encoding as its second template parameter. By default it is UTF8<> which is const char*. You have to use multibyte encoding to support multiple languages. RapidJson Supports UTF8<>, UTF16<> and UTF32<> encodings.

By specifying UTF16<> as second template parameter for PrettyWriter object in your code should solve your issue. But you will need to use std::wstring instead of std::string and const wchar_t* instead of const char* for all strings where you expect different language.