Skip to content

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

Description

@stgrzeszczak

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions