You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Whether this value has been manually approved#[derive(Debug,Clone,Deserialize,Serialize)]#[serde(bound(deserialize = "T: Deserialize<'de>"))]structMetadata<T>{/// Current, validated value of this field////// `None` if validation failedpubvalue:Option<T>,/// Value before changes were made during approval////// `None` if no changes were made#[serde(skip_serializing_if = "Option::is_none")]puboriginal_value:Option<T>,/// Whether this field has been manually vettedhas_been_vetted:bool,}
For basic use cases, this is fine. However, we need to implement SerializeAs and DeserializeAs in order to support modifications to the interior. The desired use case looks like
#[serde_as]#[derive(Debug,Clone,Deserialize,Serialize)]structUser{username:Metadata<String>,// `date_as_string::Ymd` is working fine, so we don't demo the implementation here#[serde_as(as = "Option<Metadata<date_as_string::Ymd>>")]birthday:Option<Metadata<time::Date>>,}
For that use case, we need implementations for SerializeAs and DeserializeAs:
Unfortunately, the documentation is very unclear as to how precisely to accomplish this, and I'm finding myself confused about how to proceed. At this point, it seems as though the best process might actually be to cargo expand the Serialize and Deserialize implementations, and then adjust wherever generic values are present in order to wrap them with SerializeAsWrap or DeserializeAsWrap wherever a generic parameter exists, essentially reimplementing precisely the same logic that serde already does for field naming, optional fields, etc. But that is extremely cumbersome, so hopefully there is a better way!
The text was updated successfully, but these errors were encountered:
To implement DeserializeAs and SerializeAs in this situation, one can simply use the derived Deserialize and Serialize impls by using Metadata<DeserializeAsWrap<T>> / Metadata<SerializeAsWrap<T>> and repacking the types as needed:
The SerializeAs implementation above may require copying or cloning.
If cloning is not possible or undesireable, then the type definition probably has to be duplicated and tweaked to only contain references.
Consider a generic metadata struct:
For basic use cases, this is fine. However, we need to implement
SerializeAs
andDeserializeAs
in order to support modifications to the interior. The desired use case looks likeFor that use case, we need implementations for
SerializeAs
andDeserializeAs
:Unfortunately, the documentation is very unclear as to how precisely to accomplish this, and I'm finding myself confused about how to proceed. At this point, it seems as though the best process might actually be to
cargo expand
theSerialize
andDeserialize
implementations, and then adjust wherever generic values are present in order to wrap them withSerializeAsWrap
orDeserializeAsWrap
wherever a generic parameter exists, essentially reimplementing precisely the same logic thatserde
already does for field naming, optional fields, etc. But that is extremely cumbersome, so hopefully there is a better way!The text was updated successfully, but these errors were encountered: