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

easier way to get exception reason out of json_sax_dom_callback_parser without exceptions #2024

Closed
esromneb opened this issue Apr 2, 2020 · 3 comments
Labels
kind: enhancement/improvement solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@esromneb
Copy link

esromneb commented Apr 2, 2020

  • Describe the feature in as much detail as possible.

I am forced to disable exceptions in my project as I am targeting WASM with Emscripten.

I was able to use the code below to parse json from a std::string without using any exceptions. This works great for me however if I do hit a parse error, I would like to know what the error was. Looking at class parser::parse() it uses a json_sax_dom_callback_parser, which inturn calls parse_error(). But there is no way to get the details of what happened from there when allow_exceptions is false. Is it possible to add a callback somewhere that accepts a parse_error or something like that?

  • Include sample usage where appropriate.

This is the function I'm using so far:

using nlohmann::detail::input_buffer_adapter ;
std::optional<nlohmann::json> jsonParseExceptionFree(const std::string& str) {
    const char* first = str.c_str();
    auto len = str.size();

    // The resulting json object
    nlohmann::json j;

    // some stuff I don't understand
    auto ia = std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len);
    nlohmann::detail::parser<nlohmann::basic_json<>>(ia, nullptr, false).parse(false, j);

    if( j.is_discarded() ) {
        // parse error
        return {};
    } else {
        // parsed correctly
        return j;
    }
}

Thanks for the help, I really love this library but the internals are more complicated than I understand.

@esromneb esromneb changed the title easier way to get exceptions out of json_sax_dom_callback_parser easier way to get exception reason out of json_sax_dom_callback_parser without exceptions Apr 2, 2020
@nlohmann
Copy link
Owner

nlohmann commented Apr 4, 2020

You can have it a bit simpler:

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

using json = nlohmann::json;

json jsonParseExceptionFree(const std::string& s)
{
    auto j = json::parse(s, nullptr, false);
    return j.is_discarded() ? json() : j;
}

int main() {
    std::string s = "{}}";
    auto js = jsonParseExceptionFree(s);
    std::cout << js << std::endl;
}

@nlohmann
Copy link
Owner

nlohmann commented Apr 4, 2020

Oh, now I see the actual issue. If you want more access to diagnostic information, you may need to dive into the SAX interface. You may want to have a look at the json_sax_dom_parser (https://github.com/nlohmann/json/blob/develop/include/nlohmann/detail/input/json_sax.hpp#L145) parser. The you can then access the information where the exceptions are created from.

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

esromneb commented Apr 5, 2020

Ok. I'll take a look at overriding just the parse_error as the rest of it works perfectly. Thanks!

@esromneb esromneb closed this as completed Apr 5, 2020
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