Skip to content

Conversation

@Turbo87
Copy link

@Turbo87 Turbo87 commented Nov 8, 2025

This adds a private unit_serde! macro to automatically generate unit-specific serialization and deserialization functions when the serde feature is enabled. These functions are generic over storage types (f32, f64, etc.) and can be used with serde's serialize_with and deserialize_with attributes to handle JSON values in non-base units:

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(serialize_with = "millimeter::serialize")]
    #[serde(deserialize_with = "millimeter::deserialize")]
    width: uom::si::f64::Length,
}

This adds a private `unit_serde!` macro to automatically generate unit-specific serialization and deserialization functions when the `serde` feature is enabled. These functions are generic over storage types (f32, f64, etc.) and can be used with serde's `serialize_with` and `deserialize_with` attributes to handle JSON values in non-base units:

```rust
#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(serialize_with = "millimeter::serialize")]
    #[serde(deserialize_with = "millimeter::deserialize")]
    width: uom::si::f64::Length,
}
```
@Turbo87 Turbo87 force-pushed the unit-serde-functions branch from 0c7be9c to 8aa8e47 Compare November 14, 2025 08:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant