question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Domain objects using both Gson and SugarRecord, clashing over field name "id" in Retrofit call

See original GitHub issue

Hey 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:closed
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

5reactions
JonatanSalascommented, Mar 22, 2016

@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:

@Table
public class User  {
    //If you declare as transient that field will be never serialized. 
    private transient Long id; 

    @SerializeName("id") 
    private String userId;
}

Hope this helps!

2reactions
JonatanSalascommented, Mar 23, 2016

@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.

public class SugarExclusionStrategy implements ExclusionStrategy {
    private Class<?> clazz;

    public SugarExclusionStrategy(Class<?> clazzToExclude) {
        this.clazz = clazzToExclude;
    }

    @Override
    public boolean shouldSkipClass(Class<?> clazz) {
        return false;
    }

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        return f.getDeclaringClass().equals(clazz) && f.getName().equals("id");
    }
}

And then add it when you create your Retrofit object like this:

final SugarExclusionStrategy strategy = new SugarExclusionStrategy(SugarRecord.class);

final Gson gson = new GsonBuilder()
                .addDeserializationExclusionStrategy(strategy)
                .addSerializationExclusionStrategy(strategy)
                .create();

final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(buildClient())
                .build();

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found