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
.
Gson serialization error class com.activeandroid.DatabaseHelper declares multiple JSON fields named mContext
-
23-03-2022 - |
Question
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(){}
}
La solution
Autres conseils
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();