Serialize a Uuid field in a struct to NULL if it is default() #856
-
Scanning through the available options of This is my struct: #[derive(Debug, Serialize, PartialEq, Clone)]
struct Test {
pub guid: Uuid,
} I would like to get this output, when guid has been initialized as {
"guid": null
}
Thank you for your input. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
There is nothing in the crate like this. There are no guarantees around I think something like this should work. I haven't compiled it. It basically converts the serde_with::serde_conv!(
UuidNilAsNone,
Uuid,
|uuid: &Uuid| if uuid.is_nil() {None} else Some(uuid),
|value: Option<Uuid>| -> Result<_, std::convert::Infallible> {
Ok(value.unwrap_or_default())
}
); |
Beta Was this translation helpful? Give feedback.
There is nothing in the crate like this. There are no guarantees around
Default::default()
that can be made, so there are no pre-made conversions implemented.I think something like this should work. I haven't compiled it. It basically converts the
Uuid
toOption<Uuid>
based on theis_nil
.