Module cosmic::cosmic_theme::palette::serde::as_uint

source ·
Expand description

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

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

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyColors {
    #[serde(with = "palette::serde::as_uint")]
    argb: PackedArgb,
    #[serde(with = "palette::serde::as_uint")]
    rgba: PackedRgba,
}

let my_colors = MyColors {
    argb: Srgb::new(0x17, 0xC6, 0x4C).into(),
    rgba: Srgba::new(0x17, 0xC6, 0x4C, 0xFF).into(),
};

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

assert_eq!(
    json,
    r#"{"argb":4279748172,"rgba":398871807}"#
);

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

Functions§

  • Deserialize a value from an unsigned integer.
  • Serialize the value as an unsigned integer.