-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
RFC: fluent parsing interface #1023
Comments
I'd probably just opt for an API like: server s;
s.set_host(ps.get<std::string>("hostname", [](nlohmann::json*){
throw std::invalid_argument("invalid hostname");
});
s.set_port(ps.get<std::string>("port", [](nlohmann::json *value){
if(value == null) return "irc";
throw std::runtime_error("invalid port number");
}); Or even have it be a 3-arity and have this form supported as well: s.set_port(ps.get<std::string>("port", "irc", [](nlohmann::json&){
throw std::runtime_error("invalid port number");
}); In essence have |
Did you try the current callback function? Note there will soon also be a SAX interface, see #971. |
I can't because the JSON document is already parsed when I have to destructure it. But it's a good idea too. |
If you do not need to parse, then the title is misleading... |
I am confused. Could you please elaborate in which sense you could not implement your usecase with user functions (i.e., the library's public API) or why an extension would be valuable also to other clients? |
@markand Could you elaborate? |
@markand what would be wrong with simply doing You could simply put all the validation code, exception throwing etc in |
@markand Can I close this issue? |
Feature Request
I'm searching a convenient way for destructuring data where I can have the following requirements:
Final code should look like this:
In this case, I require hostname property, if it's missing
std::invalid_argument("invalid hostname")
will be thrown. For port property, if the value is not defined, I return irc otherwise if it's defined in the wrong type I throwstd::invalid_argument("invalid port number")
.The code is not complete yet, but you can see a simple POC in this gist:
https://gist.github.com/markand/45c9f6a3f9239802850badd8869117e3
What are your thoughts? How do you usually deserialize unverified input?
The text was updated successfully, but these errors were encountered: