-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Incorrect parse error with binary data in keys? #838
Comments
I cannot reproduce the error with the #include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main(int argc, char* argv[]) {
std::ifstream f("data.json");
json j;
f >> j;
std::cout << j << std::endl;
std::string s = j.dump();
std::cout << json::parse(s) << std::endl;
} Output:
Can you please try the |
I made a testcase with the develop version that will hopefully be more useful. The issue is an invalid UTF-8 sequence being serialized it seems. Here's the test case #include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main(int argc, char* argv[]) {
std::map<std::string, std::string> m;
uint8_t key1[] = { 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127 };
std::string key1_str(key1, key1 + sizeof(key1)/sizeof(key1[0]));
m[key1_str] = "KeyUsable";
json jMap;
for (const auto& pair : m)
jMap[pair.first] = pair.second;
std::ofstream outFile("data.json", std::ios::out);
outFile << jMap;
outFile.close();
std::ifstream f("data.json");
json j;
f >> j;
std::cout << j << std::endl;
std::string s = j.dump();
std::cout << json::parse(s) << std::endl;
} And here is the error when I run it,
|
The
While serializing, the json library does not check for valid UTF-8 (only some special characters are escaped). But while deserializing it detects an invalid byte sequence. |
|
Ah ok, sorry, I should have read the "binary data" part... |
Indeed the library copies strings to the byte and does check whether they are UTF-8 encoded. The library also does not check the encoding during serialization. Therefore, the non-UTF-8 encoding does not yield an error until the serialized value is parsed again. So much for the explanation. I do not plan to implement base64 encoding (or other encodings) at the moment. We had a similar discussion about this regarding CBOR which also has some types that assume encoding/decoding. What I would like to discuss whether it would make sense to:
|
My take-away from this is that I'd be better off going with CBOR (hadn't heard of that before, thanks!).
In principle I believe errors should be reported as soon as they occur, so an exception during encoding seems good to me :) |
I agree with @charlie-ht, I would even go for an assert. This is a precondition of the library after all |
I think an assertion would be too harsh here. I'll see that I define an exception for this. |
We had a lot of issues with failing roundtrips (i.e., parse errors from serializations) in case string were stored in the library that were not UTF-8 encoded. This PR adds an exception in this case.
💥 throwing an exception in case dump encounters a non-UTF-8 string #838
Trying to serialize a non-UTF-8 encoded string now throws an exception. |
Bug Report
What is the issue you have?
I get a parse error after deserializing a map<string, string>, the serialised message was created by same version of library. The key is binary data. The error message is not helpful to find what went wrong.
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
m_message below contains the data to parse, the library seems to have done some escaping of my data but a quick inspection of the data suggests it's valid.
I provide hexdump of message above the given error. That error isn't enough for me to understand what went wrong.
A crash, please see above
arm-buildroot-linux-gnueabihf-g++.br_real (Buildroot 2017.05-git-13055-g71f35cff8-dirty) 5.4.0
develop
branch?ersion 2.1.1
No compilation error
The text was updated successfully, but these errors were encountered: