`Deserialize` is so hard to understand, anyone can help me?

Suppose I have a enum Ticker:

enum Ticker {
    Variant1,
    Variant2,
}

and two Strings:

let serialized_var1_str = "{\"Future\":{\"Comdty\":\"Variant1\"}}";
let serialized_var2_str = "{\"Future\":{\"Comdty\":\"Variant2\"}}";

How can I implement Deserialize for Ticker, so I can do this:

let var1: Ticker = serde_json::from_str(&serialized_var1_str).unwrap();
let var1: Ticker = serde_json::from_str(&serialized_var2_str).unwrap();

I've struggled a long time for this, anyone can help me?

You can separate the definition of Ticker from the definition of the data model, then implement From from the data model to Ticker and finally use #[serde(from = "...")] to implement Deserialize for Ticker by delegating to the data model.

3 Likes

Thanks very much for your help, I'll try it in that way.