Gson and Active Android: Attempted to serialize java.lang.Class. Forgot to register a type adapter? -
i'm using gson serialize active android model. model class contains primitives, , gson should have no issues serializing default settings. however, when try, error:
java.lang.unsupportedoperationexception: attempted serialize java.lang.class: <myclass>. forgot register type adapter?
i rather not write type adapter every 1 of model classes, how can around issue?
figured out. of course, active android's base model class adding fields cannot serialized default. fields can ignored using gson's excluedfieldswithoutexposeannotation()
option, follows:
gson gson = new gsonbuilder().excludefieldswithoutexposeannotation().create(); string json = gson.tojson(new book());
modify class @expose
annotation indicate fields should serialized:
@table(name = "foo") public class foo extends model { @expose @column(name = "name") public string name; @expose @column(name = "sort") public int sort; ... }