nlohmann / json

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

[json.exception.parse_error.101] parse error at line 1, column 4520: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '".\? #4352

Closed 181404010226 closed 2 months ago

181404010226 commented 2 months ago

Description

I encountered an error while trying to load the JSON shown below. The error occurred at the position highlighted in blue. image The json file: tileset.json This is a 3D Tiles project where I convert strings to UTF-8 to ensure JSON exports correctly. Without this conversion, I encounter an error labeled "invalid UTF-8 byte at index." The image below shows the problematic string in debug mode. image

Reproduction steps

when load the json file it happened.

Expected vs. actual results

I expected it successed loaded.

Minimal code example

json childJson;  
try {
    file >> childJson;
}
catch (const json::parse_error& e) {
    std::cerr << "\033[1;31mERROR!\033[0m JSON parse error in file " << entry.path() << ": " << e.what() << std::endl;
    file.close();
    return; // 中断程序
}
file.close();

Error messages

ERROR! JSON parse error in file "C:\\Users\\Administrator\\Desktop\\Data\\temp\\开元通宝及广场\\tileset.json": [json.exception.parse_error.101] parse error at line 1, column 4520: syntax error while parsing value - invalid string: forbidden character after backslash; last read: '".\?

Compiler and operating system

Microsoft Visual C++ 2022,windows10

Library version

3.10.4

Validation

Nevermore1994 commented 2 months ago

I think the string ".\开元通宝及广场" itself does not comply with the JSON specification. When exported, it should be ".\\开元通宝及广场" to comply with the specification.

nlohmann commented 2 months ago

The error message states the reason: after a backslash, only a few characters are allowed: