-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to serialize PlatformRoute, Tier, Division, etc? #33
Comments
To answer, anything that shows up in JSON responses returned by the API derive serde (though I guess only the
The easiest way to deal with this in current form would be with a newtype and serde impl, something like this: (untested) pub struct PlatformRouteSerde(pub PlatformRoute);
impl<'de> serde::de::Deserialize<'de> for PlatformRouteSerde {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>
{
let num = u8::deserialize(deserializer)?;
let route = num.parse().map_err(serde::de::Error::custom)?;
PlatformRouteSerde(route)
}
}
impl serde::ser::Serialize for PlatformRouteSerde {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serializer.serialize_u8(self.0.into())
}
} |
Thanks, I just saw the use serde_repr::{Serialize_repr, Deserialize_repr};
use serde::{Serialize, Deserialize};
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
enum SmallPrimeRepr {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[repr(u8)]
enum SmallPrime {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}
fn main() -> serde_json::Result<()> {
let j_repr = serde_json::to_string(&SmallPrimeRepr::Seven)?;
let j = serde_json::to_string(&SmallPrime::Seven)?;
println!("{} {}", j_repr, j);
let p_repr: SmallPrimeRepr = serde_json::from_str("2")?;
let p: SmallPrime = serde_json::from_str("\"Two\"")?;
println!("{:?} {:?}", p_repr, p);
Ok(())
} prints:
So I think for |
Some structures like
Match
deriveserde::Serialize
andserde::Deserialize
for parsing purpose but a few enums don't. Is there a reason?For example, I would like to create a serializable structure like this:
The text was updated successfully, but these errors were encountered: