0
struct ResponseData<T> {
    success : bool,
    res_data : T,
}
struct FooRes {
   result:RESULT,
}
num RESULT {
    RESULT_OK = 0,
    RESULT_NG = 1,
}
fn test(){

let s = ResponseData::<FooRes>{
    success : true,
    res_data : FooRes{
        result:RESULT::RESULT_OK,
    },
};
    
let st = serde_json::to_string(&s).unwrap();
println!("json={}",st);

json={"success":true,"resData":{"result":"RESULT_OK"}}

I need the outcome to be {"result":0}, not {"result":"RESULT_OK"}, when serializing the enum into a number value, and {"success":true,"resData":{"result":0}} to deserialize to the enum member result.

struct FooRes {
   result:RESULT,
}

How do I do this?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ryo-chan
  • 31
  • 2
  • Welcome to Stack Overflow. Please take some extra time to ensure that your question contains a [mre], and that the question is clear and well presented ([ask]). That code is poorly formatted and does not even compile. – E_net4 Sep 24 '20 at 10:17
  • The serde homepage on [Enum representations](https://serde.rs/enum-representations.html) might give you an idea on how to work with enum types in serde. – E_net4 Sep 24 '20 at 10:18

1 Answers1

0

Thunks, I solved

https://serde.rs/enum-number.html

Correct #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)] Not #[derive(Debug, Serialize, Deserialize)]

On Enum , tnx

Ryo-chan
  • 31
  • 2