-
Let's say I have a struct #[repr(u8)]
#[derive(Deserialize_repr)]
enum A {
Variant = 12,
} And I have JSON data, where the field is presented as an integer in a string: {
"field": "12"
} I'd like to extract the field through the sequence of deserializers: #[serde_as]
#[derive(Deserialize)]
pub struct Data {
#[serde_as(as = "...")]
field: A,
} The best approach I could find was #[serde_as(as = "JsonString<_>")]
field: A |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Why are you deriving a wrong #[repr(u8)]
#[derive(serde::Serialize)]
enum A {
#[serde(rename = "12")]
Variant = 12,
} If you don't like the way |
Beta Was this translation helpful? Give feedback.
Why are you deriving a wrong
Deserialize
implementation in the first place? If you want the enum to serialize into strings with decimal numbers, theDeserialize
implementation should match that. The normalserde_derive
is enough to achieve that:If you don't like the way
JsonString
is implemented, you could write something yourself. There is nothing in theserde_with
crate, that will allow you to chain deserializing behavior likestr -> u8
&u8 -> A
.