Skip to content

Handle multiple repeated aliases #1292

@JP-Ellis

Description

@JP-Ellis

Summary

Could or should serde_json handle input where a field has multiple aliases, and both aliases are present?

Background

I would like to support handling deserialising from either of two aliases, even in situations where both are provided.

In my particular case, we are migrating one endpoint from snake_case to camelCase, and to preserve some backwards compatibility as end-users transition, there will be an interim period where data is duplicated and returned in both formats (in a standard 'expand and contract' pattern).

As a result, we have code that looks like:

#[derive(Debug, Serialize, Deserialize)]
struct StatusResponse {
  #[serde(alias = "session_id", alias = "sessionId")]
  session_id: String
}

and we need to be able to deserialize the payload:

{
	"session_id": "123",
    "sessionId": "123"
}

Alternative

While the 'expand-and-contract' approach from the server is preferred, if the answer here is a 'no', the alternative would be to expand-and-contract from the client side, and have a hard-transition at some point from the server's perspective.

Related

I have looked into other tickets, and while I could find what I'm asking for exactly, there were some related items:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions