How to transform fields during serialization using Serde?
How can I apply a transformation to a field before serialization? For example, how can I ensure that the fields lat and lon in this struct definition are rounded to at most 6 decimal places before being serialized? #[derive(Debug, Serialize)] struct NodeLocation { #[serde(rename = "nodeId")] id: u32, lat: f32, lon: f32, } krdln The serialize_with attribute You can use the serialize_with attribute to provide a custom serialization function for your field: #[macro_use] extern crate serde_derive; extern crate serde; use serde::Serializer; fn round_serialize<S>(x: &f32, s: S) -> Result<S::Ok, S: