You can implement deserialization with the #[serde(from = "FromType")] attribute. If the transformation can fail, there is also the try_from attribute.
Deserialize this type by deserializing into FromType, then converting. This type must implement From<FromType>, and FromType must implement Deserialize.
Serializing a subset of fields can be achieved by applying the #[serde(skip)] annotation on the fields you want to omit.
Applied to your use case a solution looks like this:
#[derive(Debug, Serialize, Deserialize)] #[serde(from = "DeserS")] struct S { value: f32, #[serde(skip)] square: f32, } #[derive(Deserialize)] struct DeserS { value: f32, } impl From<DeserS> for S { fn from(tmp: DeserS) -> Self { Self { value: tmp.value, square: tmp.value.powi(2), } } }
Playground