Function cosmic::cosmic_theme::palette::serde::as_array::serialize

source ยท
pub fn serialize<T, S>(
    value: &T,
    serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where T: ArrayCast, <T as ArrayCast>::Array: Serialize, S: Serializer,
Expand description

Serialize the value as an array of its components.

use serde::Serialize;
use palette::{Srgb, Srgba};

#[derive(Serialize)]
struct MyColors {
    #[serde(serialize_with = "palette::serde::serialize_as_array")]
    opaque: Srgb,
    #[serde(serialize_with = "palette::serde::serialize_as_array")]
    transparent: Srgba,
}

let my_colors = MyColors {
    opaque: Srgb::new(0.6, 0.8, 0.3),
    transparent: Srgba::new(0.6, 0.8, 0.3, 0.5),
};

assert_eq!(
    serde_json::to_string(&my_colors).unwrap(),
    r#"{"opaque":[0.6,0.8,0.3],"transparent":[0.6,0.8,0.3,0.5]}"#
);