nlohmann / json

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

Exception thrown when dumping utf-8 characters when using std::string #4213

Closed felipegodias closed 9 months ago

felipegodias commented 10 months ago

Description

Calling dump when having a UTF-8 non ASCII character throws an exception.

Reproduction steps

  1. Create an string with "Á" value.
  2. Insert it as a value in the json.
  3. Call dump method.
  4. Exception is thrown.

Expected vs. actual results

An exception should not be thrown, the dump should contain the character without issue.

Minimal code example

std::string s = "Á";
    nlohmann::json j;
    j["value"] = s;

    try
    {
        std::string json_out = j.dump();
        std::cout << json_out << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

Error messages

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

Compiler and operating system

MSVC version '19.38.33130.0' Windows 11

Library version

3.11.2

Validation

gregmarr commented 10 months ago

Especially with MSVC, I don't think that literal string is UTF-8 encoded.

nlohmann commented 10 months ago

@felipegodias Can you please provide more information. In particular, please double-check your input is actually UTF-8 encoded.