jonasbb / serde_with

This crate provides custom de/serialization helpers to use in combination with serde's `with`-annotation and with the improved `serde_as`-annotation.
https://docs.rs/serde_with
Apache License 2.0
636 stars 67 forks source link

can I combine `#[serde(default = "default_template")]` and `#[serde_as(deserialize_as = "DefaultOnNull")]` #748

Closed LuckyTurtleDev closed 4 months ago

LuckyTurtleDev commented 4 months ago

Something like this would be nice

#[serde(default = "default_template")]
#[serde_as(deserialize_as = "DefaultOnNull")]
pub template: String,
jonasbb commented 4 months ago

I don't think this is possible. The part after deserialize_as needs to be a type/function that handles the null can call the provided function. You can do such parametrization using types, but functions are not types, but values. Basically, you cannot have a DefaultOnNull<default_template> or a default_on_null::<default_template>(). As such, I don't know how to make DefaultOnNull aware to call default_template, since it cannot store that value anywhere.