google / google/gson

class declares multiple JSON fields - How to tell Gson to consider child class field.

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

Description

Hello,

We are using [Google Ad Manager axis API](https://github.com/googleads/googleads-java-lib) in our project. While converting [LineItem](https://developers.google.com/ad-manager/api/reference/v201808/LineItemService.LineItem) object, we are getting the below error.
```
java.lang.IllegalArgumentException: class com.google.api.ads.admanager.axis.v201808.CustomCriteriaSet declares multiple JSON fields named __equalsCalc
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170) ~[gson-2.8.0.jar:?]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[gson-2.8.0.jar:?]
at com.google.gson.Gson.getAdapter(Gson.java:423) ~[gson-2.8.0.jar:?]
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[gson-2.8.0.jar:?]
```
As mentioned in [this issue](https://github.com/googleads/googleads-java-lib/issues/135), using exclusion strategy will make those fields to be null. Since those fields are mandatory, if it's sent to Ad manager we will get exception.

Hence I'm look for a way to tell Gson, instead of completely excluding the field from Deserialization, how to force Gson to consider child class fields and child class setter methods?

When it's common for a field to override it's parent class field, why not Gson consider child class's field by default? How can I overcome this exception?

Thank you.

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.