I have the following rust type:
enum SmallPrime {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}
I can deserialize using json:
[ 2, 3, 5, 7 ]
use serde_repr::*;
#[derive(Copy, Clone, Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
enum SmallPrime {
Two = 2,
Three = 3,
Five = 5,
Seven = 7,
}
fn main() {
use SmallPrime::*;
// serialize o deserialize str -> [2,3,5,7]
let j = r#"
[ 2, 3, 5, 7 ]
"#;
let vs: Vec<SmallPrime> = serde_json::from_str(j).unwrap();
println!("{}", serde_json::to_string(&vs).unwrap());
}
Does anyone know a way of simultaneously having it work with a "succeed first" strategy using an additional FromStr
option such that the following might work?
// json
[ "Two", 3, "Five", "Seven" ]
... if not supporting both of these:
// json
[ "Two", "Three", "Five", "Seven" ]
[ 2, 3, 5, 7 ]