We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An error occurs when using wstring to instantiate basic_json.
using wjson = basic_json<std::map, std::vector, std::wstring>;
Modify the json_value function in the json.hpp file. Change string = create<string_t>(""); to string = create<string_t>(); Compile
No response
Win64
3.11.3
develop
The text was updated successfully, but these errors were encountered:
https://github.com/nlohmann/json#character-encoding
To store wide strings (e.g., std::wstring), you need to convert them to a UTF-8 encoded std::string before, see an example.
#include <fstream> #include <string> #include <windows.h> #include <nlohmann/json.hpp> #include <iostream> #include <io.h> #include <fcntl.h> using json = nlohmann::json; namespace hmc_string_util { inline std::string utf16_to_utf8(const std::wstring input) { std::string result; if (input.empty()) { return result; } int inputSize = static_cast<UINT32>(input.size()); int iSizeInBytes = ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), inputSize, NULL, 0, NULL, NULL); if (iSizeInBytes > 0) { size_t char_size = static_cast<size_t>(iSizeInBytes); std::vector<char> TempResult = std::vector<char>(char_size, '\0'); ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), inputSize, &TempResult[0], static_cast<int>(char_size), NULL, NULL); result.reserve(char_size + 5); result.append(TempResult.begin(), TempResult.end()); result.push_back('\0'); } return result; } } int main() { const std::wstring _UTF16_TEXT = L"utf16宽字符車B1234 こんにちは"; const std::string _GB2312_TEXT = "utf16宽字符車B1234 こんにちは"; const std::vector<std::uint8_t> _UTF8_TEXT_T = {117,116,102,49,54,229,174,189,229,173,151,231,172,166,232,187,138,66,49,50,51,52,32,227,129,147,227,130,147,227,129,171,227,129,161,227,129,175 }; const std::string _UTF8_TEXT = std::string(_UTF8_TEXT_T.begin(), _UTF8_TEXT_T.end()); std::string utf16_to_utf8 = hmc_string_util::utf16_to_utf8(_UTF16_TEXT); json j; j["utf8"] = _UTF8_TEXT; j["utf16_to_utf8"] = utf16_to_utf8; //set CONSOLE utf8 log _setmode(_fileno(stdin), _O_U8TEXT); std::cout << j; return 0; }
Sorry, something went wrong.
No branches or pull requests
Description
An error occurs when using wstring to instantiate basic_json.
using wjson = basic_json<std::map, std::vector, std::wstring>;
Modify the json_value function in the json.hpp file.
Change string = create<string_t>("");
to string = create<string_t>();
Compile
Reproduction steps
Expected vs. actual results
Minimal code example
No response
Error messages
No response
Compiler and operating system
Win64
Library version
3.11.3
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: