Open MingweiSamuel opened 6 months ago
Sorta similar to #541, except instead of only keeping homogeneous keys this keeps the heterogeneous values
I think this implementation looks fine as is. As long as you are aware of the limitations. Since you don't specify a type for the key (but have IgnoredAny
), this only works with formats that are sufficiently self-describing to skip over unknown types (i.e., support deserialize_ignored_any
).
It would be possible to make the key type another parameter, defaulting to IgnoredAny
, but optional type parameters cannot be at the front, or rather need to be specified if any later type parameter is provided too.
There are a couple of extensions I can think to make it nicer to use.
You might not want to specify a transformation at all, but instead just use the values. In that case, you could default to Same
such that no types need to be provided. You would use it like:
#[serde_with::serde_as]
#[derive(Debug, serde::Deserialize)]
struct Foo {
#[serde_as(as = "IgnoreKeys")]
foo: (String, i32, String),
}
You could provide that by extending the macro to create a second impl for IgnoredKeys<Same>
.
Instead of only supporting heterogeneous collections, homogeneous collections would be a nice extension, similar to the #541 issue you found.
#[serde_with::serde_as]
#[derive(Debug, serde::Deserialize)]
struct Foo {
#[serde_as(as = "IgnoreKeys<_>")]
foo: Vec<String>,
}
This would need a single fully separate implementation to the macro.
I have a case where I'd like to deserialize a heterogeneous tuple from a map by ignoring the keys of the map. I implemented
DeserializeAs
forIgnoreKeys
based on the regular tuple implementation. Naturally you can only deserialize and cannot re-serialize as the key names are all lost.IgnoreKeys
```rust use std::fmt; use std::marker::PhantomData; use serde::de::{Deserializer, Error as DeError, IgnoredAny, MapAccess, Visitor}; use serde_with::de::{DeserializeAs, DeserializeAsWrap}; /// Deserialize a tuple sequence from a map, ignoring keys. pub struct IgnoreKeysIs there a better way to do this? And if not, should I make a PR to add this to the crate?