Domain objects using both Gson and SugarRecord, clashing over field name "id" in Retrofit call
See original GitHub issueHey there,
I’m wondering if there’s a workaround for this little issue I’ve encountered in a class called User, which I deserialize from a JSON object using Gson, annotating like so:
public class User extends SugarRecord{
private Long id; //used by Sugar
@SerializeName("id") //used by Gson -- I can't change this field name
private String userId;
}
Even though the variables are called totally different things and are different types in the definition of User, this addition of the Long id field with Sugar is causing Retrofit to throw an error, which says “com.package.User declares multiple JSON fields named id”.
Is there any possible workaround for this? Let me know if there’s anything else you might need to see, and thanks so much for any help you can offer!
Issue Analytics
- State:
- Created 8 years ago
- Comments:8
Top Results From Across the Web
Retrofit,Dynamic json whose fields change depending on the ...
I'm using retrofit2 and I need to call an API for information. The JSON that the API gives me has dynamic fields which...
Read more >Consuming APIs with Retrofit | CodePath Android Cliffnotes
The guide shows how to use Gson to ingest data from the Rotten Tomatoes API, but it can be used in the same...
Read more >Retrofit Android Example Tutorial - DigitalOcean
Today we'll use the Retrofit library developed by Square to handle REST API calls in our android application. Retrofit Android. Retrofit is type ......
Read more >Using Retrofit 2.x as REST client - Tutorial - Vogella.com
Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based...
Read more >The jOOQ Release Note History
This release tackles two long standing and complex feature requests that users have asked us to offer for a long time: a public...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rachellejane @MangeshKadam What you have to do is not to extend from base class SugarRecord. Instead you should use the @Table annotation which gives to you the ability to declare by yourself a private Long id.
When declaring the private Long id, you have to use the keyword transient like this:
private transient Long id;
That mean that your id field won’t be serialized NEVER!
Example class:
Hope this helps!
@rachellejane another thing that can be done when you have your model class extend from SugarRecord it’s to create an exclusion strategy for Gson.
And then add it when you create your Retrofit object like this: