palette::serde

Function serialize_as_uint

source
pub fn serialize_as_uint<T, S>(
    value: &T,
    serializer: S,
) -> Result<S::Ok, S::Error>
where T: UintCast, T::Uint: Serialize, S: Serializer,
Expand description

Serialize the value as an unsigned integer.

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

#[derive(Serialize)]
struct MyColors {
    #[serde(serialize_with = "palette::serde::serialize_as_uint")]
    argb: PackedArgb,
    #[serde(serialize_with = "palette::serde::serialize_as_uint")]
    rgba: PackedRgba,
}

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

assert_eq!(
    serde_json::to_string(&my_colors).unwrap(),
    r#"{"argb":4279748172,"rgba":398871807}"#
);