nlohmann / json

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

\ character in the content of a string cause error in parser.? #4067

Closed fanno closed 1 year ago

fanno commented 1 year ago

Description

..... "Path": { "Log": "c:\path\to\Logs\" } .....

Reproduction steps

std::ifstream i(.....); m_json = nlohmann::json::parse(data);

if the file has the \ in the variable like shown in description it fails.

Expected vs. actual results

not sure if it should be like this ? or if i need to dome something myt self when i put \ in too a variable ?

if i do\ instead of \ then it parses fine..

    std::ifstream i(filename);

    std::stringstream buffer;
    buffer << i.rdbuf(); // Read the file buffer into the stringstream
    std::string data = buffer.str();

    std::regex pattern(R"(([^\\])\\([^\\]))");
    data = std::regex_replace(data, pattern, "$1\\\\$2");

    m_json = nlohmann::json::parse(data);

this code seems to "fix" the issue

Minimal code example

file content

.....
"Path": {
            "Log": "c:\path\to\Logs\"
        }
.....

std::ifstream i(.....);
m_json = nlohmann::json::parse(data);

Error messages

i am not sure how to get this i am not so experianced with c++

Compiler and operating system

windows 10

Library version

3.11.2 single file

Validation

gregmarr commented 1 year ago

Your JSON file is malformed. The backslashes need to be escaped.

https://www.json.org/json-en.html

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

fanno commented 1 year ago

thanks, not sure how i managed to set it to this value via the interface.