-
Notifications
You must be signed in to change notification settings - Fork 4
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
Simple selective extraction of fields #16
Comments
Interesting approach to use a printf/scanf-like API. Something similar would be fairly straightforward to create with trial.protocol. Thanks for the reference. |
Another interesting C library to parse JSONs:
But this time it doesn't serve as an inspiration to Trial.Protocol. Trial.Protocol already has integration with Boost.Serialization, so nothing missing on our side. |
Another interesting C++ library. This time, the interesting bit is the |
Now that #23 was closed, I think it'll be even easier to implement this one. I think I'll implement this next (but it'll take a few weeks as I'll allocate some time to work on other stuff). What do you guys think about the following interface? int x;
boost::variant<int, std::string, boost::none_t> y;
boost::optional<std::vector<int>> z;
auto h = [](const json::reader &reader) { /* a callback */ };
boost::string_view lit;
if (!json::scan(json_view,
"foo", x,
"bar.baz", y,
"bar.a_list", z,
"bar", h,
"bar", lit)) {
std::cerr << "missing field" << std::endl;
} Every argument that is not You can pass functors receiving If you want a literal of some value, just pass a The same field can be extracted into two or more targets (then you can, for instance, get a list of strings and the literal associated with this list at once). More complex types like My greatest concern is related to JSON objects like json::scan(json_view,
"bar.foo", x,
std::make_tuple("bar", "foo"), y,
std::make_tuple("foo.bar"), z); I like this idea because the common case (no Oh, of course, Also, one last thing: I think I'd write a I'll resist the temptation to propose the name |
Just an update on my previous idea.
Therefore, I thought that this feature should be turned down. I have an update to my suggestion. Add two functions:
If user wants to read some field to multiple targets at once, he'll have to opt in explicitly like this: boost::optional<int> x;
boost::optional<std::string> y;
json::scan(json_view,
"foo.bar", [&] (json::reader &reader) {
return partial::read_to_many(reader, x, y);
}); Also, callback now receives non-const reader reference. One more feature: std::string current_key;
json::scan(json_view,
"foo.bar", x,
std::make_tuple("foo", current_key), [&](json::reader&) {
return !current_key.starts_with("_");
}); It should be self-explanatory. Wildcard match on fields not matched by other keys on nesting level of the path spec. Also, this feature (callback and matching on any nesting level or specific key I want) means I no longer see a use for #26. I'll open separate issues to track |
I still haven't given much thought to this feature, but here is some arbitrary feedback... Why should the callback signal an error with a boolean return value rather than by throwing an exception? While I do agree that we could avoid the dotted-string syntax, we should be careful not to create a solution that could be confused with JSONPath or JSON Pointer. These are better handled with recursive iterators on How does |
Using exceptions here does make sense. I'll of course change
The other difference is just cosmetic (result passed as a by-ref arg instead of return value). |
So how does |
I'm not familiar with Boost.Serialization to be honest. I went to take a more serious look now that you mentioned. It seems to me that Sorry about the late answer. |
I'm gonna focus on finishing Boost.Http parser now (expose chunk extensions and then add parsers combinators), so it'll be some time until I contribute to this project again. |
Hey,
a friend of mine ( @skhaz ) showed me a very interesting JSON library for C: https://github.com/cesanta/frozen
I thought it could be useful as an inspiration design for higher-level abstractions.
The text was updated successfully, but these errors were encountered: