android - gson read same field with different structure -
note : can achieved creating 2 different gson models, i'm looking workaround.
i have model work multiple json responses. there response :
items: [ { kind: "youtube#playlistitem", etag: ""fpj9onby0rl_lqylg6rocj9h9n8/jqbctlu8tym8b1fxgo14gnzrfg4"", id: "pluq7i1jjqkb4ljargcpwsp62l7ic06ike2lde0bxle18",
that conflict 1 (notice same id
field different type. above id
string
, other class
) :
items: [ { kind: "youtube#searchresult", etag: ""fpj9onby0rl_lqylg6rocj9h9n8/hdiu49vmd5aphkun5yz9gtljg9a"", id: { kind: "youtube#playlist", playlistid: "plh6xqivlqjsf3ynkvec1axub1dqwvgwfo" },
is possible use single gson model can read both responses?
thanks lot time
update
this inside model
, changed id
variable string
class
:
private id id; public class id { private string id; private string kind; private string playlistid; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getkind() { return kind; } public void setkind(string kind) { this.kind = kind; } public string getplaylistid() { return playlistid; } public void setplaylistid(string playlistid) { this.playlistid = playlistid; } }
this how request :
gsonbuilder gsonbuilder = new gsonbuilder(); gsonbuilder.registertypeadapter(response.class, new jsondeserializer<item.id>() { @override public item.id deserialize(jsonelement jsonelement, type type, jsondeserializationcontext jsondeserializationcontext) throws jsonparseexception { if(jsonelement.isjsonobject()) { result.setkind(jsonelement.getasjsonobject().get("kind").getasstring()); result.setplaylistid(jsonelement.getasjsonobject().get("playlistid").getasstring()); //return new item.id(jsonelement.getasjsonobject().get("kind").getasstring(), jsonelement.getasjsonobject().get("playlistid").getasstring()); return result; } else { result.setid(jsonelement.getasstring()); //return new item.id(jsonelement.getasstring()); return result; } } });
the problem
you can have id represent single type of variable, in case on first response you've got string, , on second case you've got object, has "kind" , "playlistid"
this seems weird design on backend side, isn't under control
solution
what can if think id in first, , second responses of same type create encapsulating class contain these fields like
class id { private string stringid; private string kind; private string playlistid; }
and create custom jsondeserializer based on type of json response create corresponding id object in following fashion:
new jsondeserializer<id>() { @override public id deserialize(jsonelement json, type typeoft, jsondeserializationcontext context) throws jsonparseexception { if(json.isjsonobject()) { return new id(json.getasjsonobject().get("kind").getasstring(), json.getasjsonobject().get("playlistid").getasstring()); } else { return new id(json.getasstring()); } } }
and register on gson object, handle deserialization of id fields, , depending of type base creation
or create deserializer deserialize containing object , use different fields based on type of id, should idea!
you can register jsondeserializer on gson using gsonbuilder , in gsonbuilder calling registertypeadapter type you're registering type adapter on , typeadapter want use in following way:
return new gsonbuilder().registertypeadapter(id.class, new jsondeserializer<id>() { //.. }).build();
you can read more custom typeadapters in gson documentation, here!
Comments
Post a Comment