java - Gson deserialize JSON array with multiple object types -


i have odd json like:

[   {     "type":"0",     "value":"my string"   },   {     "type":"1",     "value":42   },   {     "type":"2",     "value": {     }   } ] 

based on field, object in array type. using gson, thought have typeadapterfactory sends delegate adapters types typeadapter, i'm hung on understanding way of reading "type" field know type create. in typeadapter,

object read(jsonreader in) throws ioexception {   string type = in.nextstring();   switch (type) {     // delegate creating types.   } } 

would assume "type" field comes first in json. there decent way remove assumption?

here code wrote handle array of newsfeedarticle , newsfeedad items in json. both items implement marker interface newsfeeditem allow me check if typeadater should used particular field.

public class newsfeeditemtypeadapterfactory implements typeadapterfactory {      @override     @suppresswarnings("unchecked")     public <t> typeadapter<t> create(gson gson, typetoken<t> type) {         if (!newsfeeditem.class.isassignablefrom(type.getrawtype())) {             return null;         }          typeadapter<jsonelement> jsonelementadapter = gson.getadapter(jsonelement.class);         typeadapter<newsfeedarticle> newsfeedarticleadapter = gson.getdelegateadapter(this, typetoken.get(newsfeedarticle.class));         typeadapter<newsfeedad> newsfeedadadapter = gson.getdelegateadapter(this, typetoken.get(newsfeedad.class));          return (typeadapter<t>) new newsfeeditemtypeadapter(jsonelementadapter, newsfeedarticleadapter, newsfeedadadapter).nullsafe();     }      private static class newsfeeditemtypeadapter extends typeadapter<newsfeeditem> {          private final typeadapter<jsonelement> jsonelementadapter;         private final typeadapter<newsfeedarticle> newsfeedarticleadapter;         private final typeadapter<newsfeedad> newsfeedadadapter;          newsfeeditemtypeadapter(typeadapter<jsonelement> jsonelementadapter,                 typeadapter<newsfeedarticle> newsfeedarticleadapter,                 typeadapter<newsfeedad> newsfeedadadapter) {             this.jsonelementadapter = jsonelementadapter;             this.newsfeedarticleadapter = newsfeedarticleadapter;             this.newsfeedadadapter = newsfeedadadapter;         }          @override         public void write(jsonwriter out, newsfeeditem value) throws ioexception {             if (value.getclass().isassignablefrom(newsfeedarticle.class)) {                 newsfeedarticleadapter.write(out, (newsfeedarticle) value);              } else if (value.getclass().isassignablefrom(newsfeedad.class)) {                 newsfeedadadapter.write(out, (newsfeedad) value);              }          }          @override         public newsfeeditem read(jsonreader in) throws ioexception {             jsonobject objectjson = jsonelementadapter.read(in).getasjsonobject();              if (objectjson.has("title")) {                 return newsfeedarticleadapter.fromjsontree(objectjson);              } else if (objectjson.has("campaignname")) {                 return newsfeedadadapter.fromjsontree(objectjson);             }              return null;         }     } } 

you can register gson using following code.

return new gsonbuilder()         .registertypeadapterfactory(new newsfeeditemtypeadapterfactory())         .create(); 

Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -