0

I'm using cpprestsdk to work with JSON. During creation of JSON I have a problem with special characters like Å. For example:

json::value json;
std:string s = "ÅÅÅ";
std::wstring wstvalue(s.begin(), s.end());
json[L"key"] = json::value::string(wstvalue)

As JSON accepts only std::wstring I can't convert correctly regular string to wstring. The result of json.key is strange and not corresponds to initial ÅÅÅ value. How I can convert correctly regular std::string to std::wstring with characters like Å ?

Danny
  • 682
  • 1
  • 11
  • 21
  • JSON usually likes to be in UTF-8. Use `std:string s = u8"ÅÅÅ";`, avoid using wide string functions elsewhere in JSON. You cannot display UTF-8 in Windows, so you have to convert between UTF-8 and UTF-16 when displaying text, or when getting user input. – Barmak Shemirani Feb 18 '18 at 16:17

1 Answers1

0

Here I have founded the next solution using std::mbstowcs:

json::value json;
std:string s = "ÅÅÅ";
std::wstring wstvalue(s.size(), L' ');
wstvalue.resize(std::mbstowcs(&wstvalue[0], s.c_str(), s.size()));
json[L"key"] = json::value::string(wstvalue)
Danny
  • 682
  • 1
  • 11
  • 21