Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lyf-is-coding/f416d4ffdde7adfb6227c44afed34b92 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/f416d4ffdde7adfb6227c44afed34b92 to your computer and use it in GitHub Desktop.
Nlohmann JSON std::wstring and std::string conversion
// Nlohmann JSON doesn't support std::wstring so in order to send data in std::wstring
// convert it to utf-8 encoded std::string and then you can send it normaly.
std::string to_utf8( const std::wstring& str )
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes( str );
}
// Use this function as: from_utf8(jsData["utf8_encoded_string"].get<std::string>())
// to get std::wstring from utf-8 encoded std::string and display it to the console with std::wcout
std::wstring from_utf8( const std::string& str )
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.from_bytes( str );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment