google / google/gson

Multiple @SerializedName names choosing alternate over value.

Open
#1,544 3 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

We have run into a quirk with `@SerializedName`.

Class.java
```java
public class MyClass {
@SerializedName(value="name1",` alternate={"name2", "name3"}) String b;
}
```

Response.json
```json
{
"name1": "My Name",
"name3": "My Other Name"
}
```

In this scenario, my expectation is that GSON would choose the value `My Name` from the JSON key `name1`, because this is the expected location of the value. It seems that GSON then finds `name3` and says that is is also a valid name for `b` and overwrites the previously found value with the new one.

I'm unsure if this is due to the order of the `@SerializedName` parameters or the order of the keys in the JSON file itself.

I understand the real fix is making the endpoint returning these duplicate values at the same time, but it is surprising behavior for`@SerializedName`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.