title: GSON Deserialization of subtypes in Kotlin
body: <p>I'm not sure if this is a limitation, a bug or just bad use of GSON. I need to have a hierarchy of Kotlin objects (parent with various subtypes) and I need to deserialize them with GSON. The deserialized object has correct subtype <strong>but its field enumField is actually null</strong>. </p>
  
  <p>First I thought this is because the field is passed to the "super" constructor but then I found out that "super" works well for string, just enum is broken.</p>
  
  <p>See this example:</p>
  
  <pre><code>import com.google.gson.Gson
  import com.google.gson.GsonBuilder
  import com.google.gson.typeadapters.RuntimeTypeAdapterFactory
  
  open class Parent(val stringField: String,
                    val enumField: EnumField) {
  
      enum class EnumField {
          SUBTYPE1,
          SUBTYPE2,
          SUBTYPE3
      }
  }
  
  
  class Subtype1() : Parent("s1", EnumField.SUBTYPE1)
  class Subtype2(stringField: String) : Parent(stringField, EnumField.SUBTYPE2)
  class Subtype3(stringField: String, type: EnumField) : Parent(stringField, type)
  
  val subtypeRAF = RuntimeTypeAdapterFactory.of(Parent::class.java, "enumField")
          .registerSubtype(Subtype1::class.java, Parent.EnumField.SUBTYPE1.name)
          .registerSubtype(Subtype2::class.java, Parent.EnumField.SUBTYPE2.name)
          .registerSubtype(Subtype3::class.java, Parent.EnumField.SUBTYPE3.name)
  
  fun main() {
      val gson = GsonBuilder()
              .registerTypeAdapterFactory(subtypeRAF)
              .create()
  
      serializeAndDeserialize(gson, Subtype1()) // this works (but not suitable)
      serializeAndDeserialize(gson, Subtype2("s2")) // broken
      serializeAndDeserialize(gson, Subtype3("s3", Parent.EnumField.SUBTYPE3)) // broken
  }
  
  private fun serializeAndDeserialize(gson: Gson, obj: Parent) {
      println("-----------------------------------------")
      val json = gson.toJson(obj)
      println(json)
      val obj = gson.fromJson(json, Parent::class.java)
      println("stringField=${obj.stringField}, enumField=${obj.enumField}")
  }
  </code></pre>
  
  <p><strong>Any ideas how to achieve to deserialization of <code>enumField</code>?</strong></p>
  
  <p>(deps: <em>com.google.code.gson:gson:2.8.5</em>, <em>org.danilopianini:gson-extras:0.2.1</em>)</p>
  
  <p>P.S.: Note that I have to use <code>RuntimeAdapterFactory</code> because I have subtypes with different set of fields (I did not do it in the example so it is easier to understand).</p>