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

How to validate an input before parsing? #1336

Closed
Outstep opened this issue Nov 5, 2018 · 5 comments
Closed

How to validate an input before parsing? #1336

Outstep opened this issue Nov 5, 2018 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Outstep
Copy link

Outstep commented Nov 5, 2018

Hello,

I am working on an API type project which will receive JSON string requests. I need to validate that the input string is a good JSON string before parsing it.

I thought that I saw something in my nlohmann searching that suggests that something was going to be, or is now already, integrated into the library.

Could you please tell me where to get more information on this and possibly a simple example, as the "try-catch" method does not seem to work well for me.

Thanks :)

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2018

You can use the accept function - it takes the same parameters as parse, but only returns a boolean: true if the input is valid JSON or false otherwise. In case of an error, no exception will be thrown.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Nov 5, 2018
@Outstep
Copy link
Author

Outstep commented Nov 5, 2018

Thanks, that works great for the case in which I am validating if I have good JSON.

Now I just ran into the problem in which I need to validate that I have a JSON field "action"

I tried to use:

 string action = body["action"];

if (action.empty()) {
     cout << "Action NULL" << endl;
} else {
     cout << "Action GOOD" << endl;
}

just as a test since if I send a json like: {"test":"test"} then I get.

terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.302] type must be string, but is null

Basically, I want to now cover the case in which I have good JSON but not the correct fields

{"action" : "test" }

would be good.

How can I check for this?
Thanks again.

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2018

operator[] can only be used on objects (when used with a string key). If value body is not an object, you get the exception you described. Instead of empty(), you should use is_object() to make sure you can actually use operator[].

@nlohmann
Copy link
Owner

nlohmann commented Nov 5, 2018

(If you want to check against null, use is_null() instead of empty().)

@Outstep
Copy link
Author

Outstep commented Nov 5, 2018

Thanks for your help.

I was able to get things working with is_null() and find()
Cheers

@Outstep Outstep closed this as completed Nov 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

1 participant