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

Optional comment support. #363

Closed
qis opened this issue Nov 20, 2016 · 4 comments · Fixed by #2212
Closed

Optional comment support. #363

qis opened this issue Nov 20, 2016 · 4 comments · Fixed by #2212
Labels
kind: enhancement/improvement kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@qis
Copy link

qis commented Nov 20, 2016

Here are two basic_json functions that allow parsing JSON input with comments. This implementation is not very efficient and I would love to see this functionality implemented (either this way or properly) in nlohmann/json.

    /*!
    @copydoc parse(std::istream&, parser_callback_t)
    */
    static basic_json parse_relaxed(std::istream& i, parser_callback_t cb = nullptr)
    {
        std::stringstream ss;
        for (std::string line; std::getline(i, line);) {
            enum class state { none, escape, escape_u, escape_0, escape_1, escape_2, string, slash, end };
            state s = state::none;
            std::size_t size = 0;
            for (auto it = line.begin(), end = line.end(); s != state::end && it != end; ++it) {
                switch (s) {
                case state::none:
                    switch (*it) {
                    case '"': s = state::string; break;
                    case '/': s = state::slash; break;
                    }
                    break;
                case state::string:
                    switch (*it) {
                    case '"': s = state::none; break;
                    case '\\': s = state::escape; break;
                    }
                    break;
                case state::escape:
                    switch (*it) {
                    case 'u': s = state::escape_u; break;
                    default: s = state::string; break;
                    }
                    break;
                case state::escape_u:
                case state::escape_0:
                case state::escape_1:
                case state::escape_2:
                    if (*it < '0' || *it > '9') {
                        throw std::domain_error("invalid unicode escape sequence");
                    }
                    s = static_cast<state>(static_cast<int>(s) + 1);
                    break;
                case state::slash:
                    if (*it != '/') {
                        throw std::domain_error("invalid comment syntax");
                    }
                    size--;
                    s = state::end;
                    continue;
                case state::end:
                    break;
                }
                size++;
            }
            ss.write(line.data(), size);
        }
        return parse(ss, cb);
    }

    /*!
    @copydoc parse_relaxed(std::istream&, parser_callback_t)
    */
    static basic_json parse_relaxed(const string_t& s, parser_callback_t cb = nullptr)
    {
        return parse_relaxed(std::istringstream(s), cb);
    }
@nlohmann
Copy link
Owner

For this library, we do not plan to support any input which is not compliant to RFC 7159.

See #311, #294, #192.

@nlohmann
Copy link
Owner

(If you really need to skip comments, you may want to add rules to the re2c part - this would be more efficient and would work in all parsers.)

@qis
Copy link
Author

qis commented Nov 20, 2016

@nlohmann Thanks for the tip but I like to distinguish between compliant input generated by JavaScript and other libraries and configuration files written by hand.

@nlohmann
Copy link
Owner

@qis I understand that comments may be convenient, but this would be a noncompliant extension which would result in troubles later on.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 23, 2020
@nlohmann nlohmann reopened this Jun 23, 2020
@nlohmann nlohmann added this to the Release 3.8.1 milestone Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants