Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use decodeString in jsoncpp to decode a string containing unicode characters

I have read all the jsoncpp documentation here and I know how to use jsoncpp for char * and std::string but, I need a way to obtain wchar data from my json file. I am guessing this can be done using the decodeString function present in json_reader.cpp. The documentation doesn't describe whether the Token is an in paramter or an out parameter or how exactly am i supposed to obtain that Token, considering it's an [in] parameter. I have searched for over 2 days now and I can't seem to find a lead.

Any sort of suggestions or links are highly welcome.

like image 537
Nikita Rana Avatar asked Jun 03 '26 10:06

Nikita Rana


1 Answers

Windows uses UTF16 UNICODE standard. New Windows projects should be setup with UNICODE settings.

Linux and internet net based systems use UTF8 standard.

You receive data through json, it is probably in UTF8, available as std::string or const wchar. Convert that to UTF16. Using:

std::string get_utf8(const std::wstring &wstr)
{
    if (wstr.empty()) return std::string();
    int sz = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), 0, 0, 0, 0);
    std::string res(sz, 0);
    WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &res[0], sz, 0, 0);
    return res;
}

std::wstring get_utf16(const std::string &str)
{
    if (str.empty()) return std::wstring();
    int sz = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), 0, 0);
    std::wstring res(sz, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &res[0], sz);
    return res;
}

For example,

std::string fromJSON = getjson(...);
std::wstring ws = get_utf16(fromJSON);

Now Windows can display ws

MessageBox(0, ws.c_str(), 0, 0);

Convert it back to UTF8 before sending it to JSON:

std::string str = get_utf8(ws);
set_JSON_string(str);
like image 122
Barmak Shemirani Avatar answered Jun 05 '26 02:06

Barmak Shemirani