Is there a way to add custom per-element parsers to std::vectors? #1658
joshuamaiche
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
|
Thanks for sharing this solution! I really like this approach and would be glad to add it as a tool for Glaze. Feel free to add a pull request, or I'll get to it when I'm able. It will just need some unit tests and documentation. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
There are a number of situations where parsing a vector can't be done by inserting a blank object then parsing the object:
These situations all seem bespoke, and best handled by the user. For example, here's a simple struct:
And here's a wrapper that can insert into a vector of
ImmutableStructs:However, I didn't see a great "batteries included" way in Glaze to run this wrapper on every element. I ended up making my own, with a few requirements:
glz::json_t.What I ended up was basically a type that looks like a vector, but is actually just a passthrough to an adapter, that does whatever it wants on a per-element basis:
Usage is pretty much what you would expect:
Here's a godbolt link showing the full example: https://godbolt.org/z/ahbzbKKMf
I still have an issue with this implementation--it depends on just passing whatever concepts that indicate that this should be deserialized like a vector. When Glaze bumped up to V5, for instance, I had to modify the code to comply with Glaze's internals. This leads to my question.
Is there a format-agnostic way to pass a wrapper to every element being parsed in an array? If not, could something like
per_element_adapterbe added to Glaze, so I don't have to maintain it on my side? I don't know what typical Glaze usage is, but I would imagine other users would also benefit from controlling how elements are inserted into their array-like collections.Beta Was this translation helpful? Give feedback.
All reactions