Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the Gson project code base is <a href="https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java" rel="noreferrer">the RuntimeTypeAdapter</a>, which reportedly works well for polymorphic serialization and deserialization. I don't think I've yet tried to use it. See <a href="http://code.google.com/p/google-gson/issues/detail?id=231" rel="noreferrer">http://code.google.com/p/google-gson/issues/detail?id=231</a> for more info. Note, it hasn't yet been included in any Gson releases.</p> <p>If use of it doesn't fit your needs, then custom deserialization processing is necessary. Following is one such approach, assuming you want to use the JSON structure demonstrated. (I'd take a somewhat different approach, if the JSON structure could be different.)</p> <pre><code>import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; public class App { public static void main(String[] args) { Barn[] barns = {new Barn(), new Barn()}; barns[0].type = "horse"; barns[0].animal = new Horse(); barns[1].type = "cow"; barns[1].animal = new Cow(); String json = new Gson().toJson(barns); // [{"type":"horse","animal":{}},{"type":"cow","animal":{}}] BarnDeserializer deserializer = new BarnDeserializer("type"); deserializer.registerBarnType("horse", Horse.class); deserializer.registerBarnType("cow", Cow.class); Gson gson = new GsonBuilder().registerTypeAdapter(Barn.class, deserializer).create(); List&lt;Barn&gt; barns2= gson.fromJson(json, new TypeToken&lt;List&lt;Barn&gt;&gt;(){}.getType()); for (Barn barn : barns2) { System.out.println(barn.animal.getClass()); } } } class BarnDeserializer implements JsonDeserializer&lt;Barn&gt; { String barnTypeElementName; Gson gson; Map&lt;String, Class&lt;? extends Animal&gt;&gt; barnTypeRegistry; BarnDeserializer(String barnTypeElementName) { this.barnTypeElementName = barnTypeElementName; gson = new Gson(); barnTypeRegistry = new HashMap&lt;&gt;(); // Java 7 required for this syntax. } void registerBarnType(String barnTypeName, Class&lt;? extends Animal&gt; animalType) { barnTypeRegistry.put(barnTypeName, animalType); } @Override public Barn deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject barnObject = json.getAsJsonObject(); JsonElement animalTypeElement = barnObject.get(barnTypeElementName); Barn barn = new Barn(); barn.type = animalTypeElement.getAsString(); Class&lt;? extends Animal&gt; animalType = barnTypeRegistry.get(barn.type); barn.animal = gson.fromJson(barnObject.get("animal"), animalType); return barn; } } class Barn {String type; Animal animal;} class Animal {} class Horse extends Animal {} class Cow extends Animal {} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload