Rust - #[serde(tag="a.x")] attribute on a sub field in JSON

Viewed 24

I have these two structs:

#[derive(Debug, Deserialize, Serialize)]
struct RequestBase {
    id: i32,
    method_name: String,
}


#[derive(Debug, Deserialize, Serialize)]
struct LoginRequest {
    base: RequestBase,

    // Login
    email: String,
    password: String,
}

#[derive(Debug, Deserialize, Serialize)]
struct CreateAccountRequest{
    base: RequestBase,

    // Account
    email: String,
    password: String,
}

I want to map a json from ist "base.method_name" property.

I tried to use #[serde(tag="base.method_name")] but it can't access a sub property which is not in first scope.

An example of JSON:

// This should map to LoginRequest struct.
{"base":{"id":1,"method_name":"LoginRequest"},"email":"a@mail.com","password":"xxxx"}

// This should map to CreateAccountRequest struct.
{"base":{"id":1,"method_name":"CreateAccountRequest"},"email":"a@mail.com","password":"xxxx"}

How can I do this? How can I separate LoginRequest and CreateAccountRequest (which has same fields) structs from base.method_name?

Thanks!

0 Answers
Related