The system uses a custom Deserialize implementation for the main NodeServiceData type to route incoming data to the correct versioned struct based on the schema_version field.
- Latest versions typically use a custom
deserialize_vX() method to handle complex logic. - Older versions use standard
serde::Deserialize via serde_json::from_value(). - Routing logic matches the
schema_version and then performs a conversion (using Into or From) to upgrade the data to the current latest version.
impl<'de> Deserialize<'de> for NodeServiceData {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// Check schema_version and route to appropriate deserializer
match schema_version {
Some(2) => {
// Use custom deserialize_v2() instead of derived Deserialize
match super::node_service_data_v2::NodeServiceDataV2::deserialize_v2(
&mut serde_json::de::Deserializer::from_str(&json_value.to_string()),
) {
Ok(v2) => Ok(v2),
Err(e) => Err(D::Error::custom(format("Failed to deserialize as V2: {}", e))),
}
}
// ... handle other versions
}
}
}