Skip to content
New issue

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

whit std::wstring compile error #4460

Open
2 tasks
aSurgingRiver opened this issue Sep 18, 2024 · 1 comment
Open
2 tasks

whit std::wstring compile error #4460

aSurgingRiver opened this issue Sep 18, 2024 · 1 comment

Comments

@aSurgingRiver
Copy link

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

image

Reproduction steps

image

Expected vs. actual results

image

Minimal code example

No response

Error messages

No response

Compiler and operating system

Win64

Library version

3.11.3

Validation

@kihlh
Copy link

kihlh commented Oct 13, 2024

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants