An interesting task ...
The first thing that comes to mind is to transfer 2 objects to the Bundle , one shows the type of json, and the second is json itself:
//ΠΎΡΠΏΡΠ°Π²ΠΊΠ° ModelA modelA; bundle.putInt("type", 1); //ΡΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΡΠΈΠΏ bundle.putString("json", gson.toJson(modelA, ModelA.class); //ΠΏΠΈΡ
Π°Π΅ΠΌ ΡΡΡΠΎΠΊΡ json //ΠΏΡΠΈΠ΅ΠΌΠ½ΠΈΠΊ int type=bundle.getInt("type") String jsonString=bundle.getString("json"); Model model; switch(type) { case 1: model=gson.parseJson(jsonString, ModelA.class); break; case 2: model=gson.parseJson(jsonString, ModelB.class); break; case 3: model=gson.parseJson(jsonString, ModelC.class); break; default: throw new IllegaArgumentException("blah-blah); }
Update
I remembered how I solved a similar task in my project. Create a custom Gson / json serializer, the task of which is to insert the object type during serialization and its use when deserializing:
public class ModelSerializer<T extends Model> implements JsonSerializer<T>, JsonDeserializer<T> { private static final String CLASS_META_KEY = "clz"; private final Gson defaultGson; public ModelSerializer() { defaultGson=new Gson(); //Π½ΡΠΆΠ΅Π½ Π³ΠΎΠ»ΡΠΉ ΡΠΊΠ·Π΅ΠΌΠΏΠ»ΡΡ Gson } @Override public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { JsonElement element=null; if (src == null) { return element; } if (src instanceof ModelA) element = context.serialize(src, ModelA.class); else if (src instanceof ModelB) element = context.serialize(src, ModelB.class); else if (src instanceof ModelC) element = context.serialize(src, ModelC.class); //Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π² Json ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ - ΠΏΠΎΠ»Π½ΠΎΠ΅ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΊΠ»Π°ΡΡΠ° element.getAsJsonObject().addProperty(CLASS_META_KEY, src.getClass().getCanonicalName()); return element; } @Override public T deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Class<?> clz; T model; JsonObject object = jsonElement.getAsJsonObject(); if(object.has(CLASS_META_KEY)) { //ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅ΠΌ ΡΠΈΠΏ String className = object.get(CLASS_META_KEY_RECORD).getAsString(); try { clz = Class.forName(className); } catch (Exception e) { //blah-blah } model = context.deserialize(jsonElement, clz); } else { model=defaultGson.fromJson(jsonElement, typeOfT); } return model; } }
Now we stick this handler in Gson:
gson = new GsonBuilder() .registerTypeAdapter(Model.class, new ModelSerializer<Model>()) .serializeNulls() //.setPrettyPrinting() //for debugging .disableHtmlEscaping() .create();
further we use this copy of Gson for serialization and deserialization.
With this approach, the entire circus with horses with a type assignment in a separate int type and so on. not needed.