-
Notifications
You must be signed in to change notification settings - Fork 2
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
Option<Vec> support #8
Comments
I'd agree with this, but I'm also not sure the best way to handle it. One way is just an I'm curious about |
It would have to be added as a dependency but then that would work out of the box I think. I guess that could also be PR-ed there. Alternately there could be a design inspired by serde_bytes, where their function applies for several different types via an internal Deserialize trait. Alternately again, one could write a function that takes any Serialize/returns any Deserialize<'de>, and write a struct that wraps the original (de)serializer, and implements (De)Serialize, converting the top-level map into a seq (resp. seq into a map). This could then use the deserialize_option hint to properly propagate some/none. This would be arguably less type-safe, as we would only require a deserialize impl, but that would also be more flexible as that would allow for any output type that supports sequences of tuples, not only Vecs. That would not seem to really be less typesafe than serde otherwise is anyway though, as you need your struct to match the data anyway, and there are already a bunch of always-erroring combinations that serde lets through at compile time depending on the format so you should generally test deserialization anyway. |
Oh, interesting. I quite like this!
Interesting - this could definitely be more general. I'd probably prioritize type-safety, though. As you note, serde in general isn't the most type safe system, but generally if you take the recommended paths, and only ever convert data to/from the same structs, it's just about as type safe as a serialization system can get. Granted, this wouldn't necessarily have to change that - I mean it could be something like "the outermost sequence of tuples found is converted into a map", and if there is none, silently fail doing nothing. Still, I think a type error would be better. And maybe we could allow for something like arbitrary sequence types by doing a blanket implementation of Back to |
It would be nice to have a way to make it work with Option as well, either via
serde_with
'sserde_as
framework, or via a dedicated functionThe text was updated successfully, but these errors were encountered: