Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception message for duplicate field names #2251

Conversation

Marcono1234
Copy link
Collaborator

This hopefully makes it easier to troubleshoot this issue, especially when there is a conflict between @SerializedName and an inherited field, or when users depend on Java or Android platform types. In the latter case it will be easier as external person to spot the underlying cause of the issue (i.e. serializing third-party types).

@Marcono1234 Marcono1234 force-pushed the marcono1234/duplicate-fields-exception-improvements branch from cb60f01 to 4b2ee88 Compare December 3, 2022 21:01
@@ -310,14 +310,16 @@ private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type,

static abstract class BoundField {
final String name;
final Field field;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As side note: Most likely BoundField (or rather its anonymous subclass) already had an implicit Field field since it captured the Field from the enclosing scope:

return new ReflectiveTypeAdapterFactory.BoundField(name, field.getName(), serialize, deserialize) {
@Override void write(JsonWriter writer, Object source)
throws IOException, IllegalAccessException {
if (!serialized) return;
if (blockInaccessible) {
if (accessor == null) {
checkAccessible(source, field);

@eamonnmcmanus
Copy link
Member

This looks great, thanks!

@eamonnmcmanus eamonnmcmanus merged commit 6c27553 into google:master Dec 5, 2022
@Marcono1234 Marcono1234 deleted the marcono1234/duplicate-fields-exception-improvements branch December 5, 2022 21:09
tibor-universe pushed a commit to getuniverse/gson that referenced this pull request Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants