nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.97k stars 6.72k forks source link

[json.exception.type_error.316] invalid UTF-8 byte at index 0: 0xB6 #3879

Closed zhjr2019 closed 1 year ago

zhjr2019 commented 1 year ago

Description

int main()
{
    try
    {
        std::ifstream replyfile("D:\\REPLY.bson", std::ios::in | std::ios::binary);
        if (!replyfile.is_open())
        {
            return EXIT_FAILURE;
        }

        std::istreambuf_iterator<char> beg(replyfile), end;
        std::string replycontent = std::string(beg, end);
        replyfile.seekg(0, std::ios::end);
        nlohmann::json replyjson = nlohmann::json::from_bson(replycontent);
        std::cout << replyjson << std::endl;

        replyfile.close();
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }

    system("pause");
    return 0;
}

output: image REPLY.bson:有中文 image REPLY.bson file: REPLY.zip

Reproduction steps

打开包含中文字符的bson文件。

Expected vs. actual results

能正确读写包含中文字符的bson文件。

Minimal code example

No response

Error messages

[json.exception.type_error.316] invalid UTF-8 byte at index 0: 0xB6

Compiler and operating system

windows11

Library version

3.11.2

Validation

nlohmann commented 1 year ago

There is an error and index 0 which means already the first byte is invalid. The spec (https://bsonspec.org/spec.html) does not allow byte 0xB6 as first byte.

When I look at your file, the first byte is 0x6E, however. Can you please double check your example?

nlohmann commented 1 year ago

@zhjr2019 Any update?

zhjr2019 commented 1 year ago

@nlohmann I don't know how to modify it. Can you teach me?