Created
May 5, 2023 16:38
-
-
Save lyf-is-coding/f416d4ffdde7adfb6227c44afed34b92 to your computer and use it in GitHub Desktop.
Nlohmann JSON std::wstring and std::string conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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