Skip to content
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

Misleading error message - unexpected '"' - on incorrect utf-8 symbol #802

Closed
stgrzeszczak opened this issue Oct 27, 2017 · 4 comments
Closed
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@stgrzeszczak
Copy link

stgrzeszczak commented Oct 27, 2017

Hi,
I was recently confounded by this error and spent quite a while looking for unmached quotation marks, only to find out that my JSON had incorrect utf-8 data. Please take a look at the code sample below. I have a wrapper file for your library that looks like this:

namespace json {
using uint_t = std::uint64_t;
using int_t = std::int64_t;
using string_t = std::string;
template <typename T, typename... Args> using array_t = std::vector<T>;
template <typename K, typename T, typename... Args> using object_t = std::map<K, T>;
using bool_t = bool;
using float_t = double;

using Json = nlohmann::basic_json<object_t, array_t, string_t, bool_t, int_t, uint_t, float_t>;
}

and here's an example program showing the issue:

#include "json-wrapper/json-wrapper.hpp"

int main(void) {
    std::string data{};
    data += char(0xff);   // 'data' string is not UTF-8 compliant

    json::Json message{};
    message["data"] = data;

    std::string raw_message = message.dump();

    try {
        json::Json parsed = json::Json::parse(raw_message);
    }
    catch (const std::exception& e) {
        std::cout << e.what() << '\n';
    }

    return 0;
}

The output is parse error - unexpected '"'. It should say that there was a problem parsing unicode characters.

@nlohmann
Copy link
Owner

With the latest version in the develop branch, the exception reads:

[json.exception.parse_error.101] parse error at 10: syntax error - invalid string: ill-formed UTF-8 byte; last read: '"\377'

Octal 0377 is 0xFF. The error message could be nicer, but at least it properly indicates the position and the fact that it is ill-formed UTF-8.

@nlohmann
Copy link
Owner

Could you try with the latest develop version?

@stgrzeszczak
Copy link
Author

Hi, thanks for the quick response. I'll give it a try later today.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 27, 2017
@stgrzeszczak
Copy link
Author

stgrzeszczak commented Oct 27, 2017

Hello again! I've tried the develop branch and it works just as you stated. Nice!
So, I guess I'll close this issue, because it's already solved :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants