serde

How can I support an unknown or other value for a Serde enum?

冷暖自知 提交于 2021-01-19 04:04:32
问题 I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum PortType { Sctp, Tcp, Udp, } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct PortMapping { pub private_port: u16, pub public_port: u16, #[serde(rename = "Type")] pub port_type: PortType, }

How can I support an unknown or other value for a Serde enum?

﹥>﹥吖頭↗ 提交于 2021-01-19 04:02:07
问题 I have a JSON API that returns an object that looks like this: { "PrivatePort": 2222, "PublicPort": 3333, "Type": "tcp" } To capture this, I have an enum and a struct: #[derive(Eq, PartialEq, Deserialize, Serialize, Debug)] #[serde(rename_all = "snake_case")] pub enum PortType { Sctp, Tcp, Udp, } #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "PascalCase")] pub struct PortMapping { pub private_port: u16, pub public_port: u16, #[serde(rename = "Type")] pub port_type: PortType, }

How to use Serde to parse a field that might fail to be deserialized without failing the entire deserialization?

我与影子孤独终老i 提交于 2020-11-29 09:41:57
问题 I am deserializing some JSON objects which come in as requests. The input body is nested, but a certain field is sometimes misformatted for a variety of reasons. In that situation I still want the rest of the object. This doesn't all have to be done through serde; but what is happening now, is that if a single subfield is messed up, the whole request is trashed. I want to somehow still deserialize that result and just mark the field as errored out. How can this be done? E.g. the data schema

How to use Serde to parse a field that might fail to be deserialized without failing the entire deserialization?

百般思念 提交于 2020-11-29 09:40:05
问题 I am deserializing some JSON objects which come in as requests. The input body is nested, but a certain field is sometimes misformatted for a variety of reasons. In that situation I still want the rest of the object. This doesn't all have to be done through serde; but what is happening now, is that if a single subfield is messed up, the whole request is trashed. I want to somehow still deserialize that result and just mark the field as errored out. How can this be done? E.g. the data schema