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

Allow to use non strict parsing #2063

Closed
mlutken opened this issue Apr 25, 2020 · 4 comments
Closed

Allow to use non strict parsing #2063

mlutken opened this issue Apr 25, 2020 · 4 comments
Labels
kind: enhancement/improvement solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@mlutken
Copy link

mlutken commented Apr 25, 2020

Currently, when parsing json from the "wild-web" there might be small problems like for example an ';' appended after an otherwise correct json. Being able to create a parser that flips the internal strict mode to false would be a great help when parsing these kinds of json sources.

The
"static basic_json parse" function around line 6608 in json.hpp calls the internal parser with a "hardcoded" 'true' for the strict mode parameter.
If we could somehow make it possible to instantiate a json parser which uses false here, that would be really useful.

The function looks like this:

    JSON_HEDLEY_WARN_UNUSED_RESULT
    static basic_json parse(detail::input_adapter&& i,
                            const parser_callback_t cb = nullptr,
                            const bool allow_exceptions = true)
    {
        basic_json result;
        parser(i, cb, allow_exceptions).parse(true, result);
        return result;
    }

It's the parser(i, cb, allow_exceptions).parse(true, result); I would like to be able to be:

parser(i, cb, allow_exceptions).parse(false, result);

Regards Martin

@nlohmann
Copy link
Owner

You're right, this bool is not public. In order to use it, you would need to use the SAX parser directly:

#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string input = "{};";

    json result;
    nlohmann::detail::json_sax_dom_parser<json> sdp(result, true);
    auto parse_result = json::sax_parse(input, &sdp, nlohmann::detail::input_format_t::json, false);
    
    if (parse_result)
    {
        std::cout << result << std::endl;
    }
}

Output:

{}

@mlutken
Copy link
Author

mlutken commented Apr 25, 2020

That works perfectly. Thanks a lot.
But is the details namespace meant for public use?

@nlohmann
Copy link
Owner

You're right. So far, we make no guarantees about stability of the detail namespace.

What is stable though is the following:

#include <iostream>
#include <sstream>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string input = "{};";

    json result;
    
    std::stringstream ss;
    ss << input;
    ss >> result;
    
    std::cout << result << std::endl;
}

Parsing from a stream via operator>> will always use the non-strict parser and does not rely on the detail namespace.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 26, 2020
@mlutken
Copy link
Author

mlutken commented Apr 26, 2020

OK, that's good to know. So if in the future there will be no alternative to you first solution I can allways fallback to this one.
Again, thanks a lot for your great library :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement 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