Module cosmic::cosmic_theme::palette::serde::as_array

source ·
Expand description

Combines serialize_as_array and deserialize_as_array as a module for #[serde(with = "...")].

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

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyColors {
    #[serde(with = "palette::serde::as_array")]
    opaque: Srgb,
    #[serde(with = "palette::serde::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),
};

let json = serde_json::to_string(&my_colors).unwrap();

assert_eq!(
    json,
    r#"{"opaque":[0.6,0.8,0.3],"transparent":[0.6,0.8,0.3,0.5]}"#
);

assert_eq!(
    serde_json::from_str::<MyColors>(&json).unwrap(),
    my_colors
);

Functions§

  • Deserialize a value from an array of its components.
  • Serialize the value as an array of its components.