Gson serialization error class com.activeandroid.DatabaseHelper declares multiple JSON fields named mContext
Asked Answered
D

2

5

The error im getting is "java.lang.IllegalArgumentException: class com.activeandroid.DatabaseHelper declares multiple JSON fields named mContext"

I am using AndroidAnnotations RestClient to pull data from my Web Service and serialize into POJOs. The serializations worked fine while using ORMLite but i recently decided to try out Active Android and now my classes extends Model. Gson is serializing the parent class which i have no control over. Any way I can only include certain fields or maybe just return plain JSON from the RestClient and do the serialization a different way

@Rest(rootUrl = "http://stuff...com", converters = { GsonHttpMessageConverter.class })
    public interface RestClient {
    @Get("/AndroidController/createFacebookUser?facebookToken={facebookToken}&catIds=    {catIds}")
    User createFacebookUser(String facebookToken,String catIds);
}

and the User model is

@Data
@Table(name = "Users")
public class User extends Model {
@Column(name = "SystemID")
private String systemID;
@Column(name = "Name")
private String name;
public List<GameEntry> items() {
    return getMany(GameEntry.class, "Category");
}

public User(){}

}

Deprive answered 12/3, 2013 at 4:34 Comment(0)
B
7

Gson provides multiple ways of excluding fields and types from Serialization and Deserialization. Take a look at Excluding Fields From Serialization and Deserialization, specifically the usage of @Expose annotation and user-defined ExclusionStrategy.

Basra answered 12/3, 2013 at 5:7 Comment(0)
D
4

I Created my own Serializer which extended AbstractHttpMessageConverter following the answer here

Custom HttpMessageConverter with @ResponseBody to do Json things

Only thing i changed was that i made the Gson and used the @Expose on the fields i wanted serialized

private Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Deprive answered 13/3, 2013 at 1:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.