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

JSON Pointer resolve failure resulting in incorrect exception code #888

Closed
cmannett85 opened this issue Dec 22, 2017 · 9 comments
Closed
Assignees
Labels
confirmed documentation solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@cmannett85
Copy link

cmannett85 commented Dec 22, 2017

int main()
{
    auto model = R"({
        "a": {
            "b": 42
        }
    })"_json;

    try {
        model.at("/a/c"_json_pointer);
    } catch (json::exception& e) {
        std::cout << "ID: " << e.id << std::endl;
    }

    return EXIT_SUCCESS;
}

Prints:

ID: 403

According to the json::out_of_range and json::at(const json_pointer&) docs, a JSON pointer that cannot be resolved should return a 404, but it actually returns a 403.

@nlohmann
Copy link
Owner

Thanks for the info! I'll check.

@nlohmann nlohmann self-assigned this Dec 22, 2017
nlohmann added a commit that referenced this issue Dec 23, 2017
The at function throws json::out_of_range.403 when a nonexistent object key is provided (just like the usual at function). This was not documented and users could assume json::out_of_range.404 would be thrown instead.

- Updated documentation.
- Added example code.
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Dec 23, 2017
@nlohmann nlohmann added this to the Release 3.0.1 milestone Dec 23, 2017
@nlohmann
Copy link
Owner

@cmannett85 Can you please have a look whether the updated documentation better describes the behavior of the function?

@cmannett85
Copy link
Author

cmannett85 commented Dec 24, 2017

@nlohmann, can you explain the difference between a 403 and 404 error? In the example code:

    // out_of_range.403
    try
    {
        // try to use a JSON pointer to an nonexistent object key
        json::const_reference ref = j.at("/foo"_json_pointer);
    }
    catch (json::out_of_range& e)
    {
        std::cout << e.what() << '\n';
    }

    // out_of_range.404
    try
    {
        // try to use a JSON pointer that cannot be resolved
        json::reference ref = j.at("/number/foo"_json_pointer);
    }
    catch (json::out_of_range& e)
    {
        std::cout << e.what() << '\n';
    }

In the latter example foo is a non-existent object key in number, so why does not it not also throw a 403?

@nlohmann
Copy link
Owner

Take

{"one": 1, "two": 2, "list": [1,2,3]}

as example.

  • JSON Pointer /bar yields 403, because / is an object, but key bar was not found.
  • JSON Pointer /list/foo yields 404, because /list is an array, and foo makes no sense here.

@cmannett85
Copy link
Author

Ah, so a 403 is always thrown when a missing object member is requested, whilst a 404 is thrown when the JSON pointer is invalid (as opposed to 'malformed')? If so, why does the last example in your link throw a 404 instead of a 403 (as number is an object)?

@nlohmann
Copy link
Owner

In the example, / is an object, /number is the number 1, so /number/foo fails, because a number is not an object, so there cannot be a key foo.

@cmannett85
Copy link
Author

I finally understand! Your documentation update addresses the issue, so I'll close this issue.

@nlohmann
Copy link
Owner

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed documentation 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