palette::serde

Function deserialize_as_uint

source
pub fn deserialize_as_uint<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where T: UintCast, T::Uint: Deserialize<'de>, D: Deserializer<'de>,
Expand description

Deserialize a value from an unsigned integer.

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

#[derive(Deserialize, PartialEq, Debug)]
struct MyColors {
    #[serde(deserialize_with = "palette::serde::deserialize_as_uint")]
    argb: PackedArgb,
    #[serde(deserialize_with = "palette::serde::deserialize_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 = r#"{"argb":4279748172,"rgba":398871807}"#;
assert_eq!(
    serde_json::from_str::<MyColors>(json).unwrap(),
    my_colors
);