Skip to main content

serialize

Function serialize 

Source
pub fn serialize<T, S>(
    value: &Option<T>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where T: Serialize, S: Serializer,
Expand description

Serialize an optional value as a singleton map.

For Some(value), this serializes the value using singleton map format. For None, this serializes as null.

ยงExamples

use noyalib::with::singleton_map_optional;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
enum Action {
    Start,
    Stop,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Command {
    #[serde(
        with = "singleton_map_optional",
        skip_serializing_if = "Option::is_none",
        default
    )]
    action: Option<Action>,
}