title: How to polymorphically deserialize a class based in two properties?
body: <p>I'm having problems trying to deserialize from json string into the correct object since I have to check 2 properties before deserialize to the correct class, i.e.:</p>
  <pre><code>{
    &quot;org&quot;: &quot;Company1&quot;,
    &quot;event_type&quot;: &quot;REGISTER&quot;,
    &quot;day&quot;: &quot;2021-01-01&quot;,
    &quot;data&quot;: {
      &quot;name&quot;: &quot;Kyore&quot;,
      &quot;age&quot;: 10
    }
  },
  {
    &quot;org&quot;: &quot;Company2&quot;,
    &quot;event_type&quot;: &quot;REGISTER&quot;,
    &quot;day&quot;: &quot;2021-01-01&quot;,
    &quot;data&quot;: {
      &quot;name&quot;: &quot;Casky&quot;,
      &quot;age&quot;: 12,
      &quot;lastName&quot;: &quot;Kyky&quot;
    }
  },
  {
    &quot;org&quot;: &quot;Company2&quot;,
    &quot;event_type&quot;: &quot;DELETE&quot;,
    &quot;day&quot;: &quot;2021-01-01&quot;,
    &quot;data&quot;: {
      &quot;id&quot;: &quot;1234-1234-1234&quot;,
      &quot;reason&quot;: &quot;user requested&quot;
    }
  }
  </code></pre>
  <p>I could make it work with the basic polymorphic deserialization using the JsonTypes as you can see <a href="https://stackoverflow.com/a/68363521/1179274">here</a></p>
  <p>The problem now is that I have this &quot;org&quot; field that might give me the same &quot;event_type&quot; value, but with a different data, so I need to find a way to first check if <code>org</code> is either Company1 or Company2 to call the correct JsonSubType, ie:</p>
  <pre><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = &quot;org_id&quot;, include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
  @JsonSubTypes(value = [
      JsonSubTypes.Type(Company1Data::class, name = &quot;Company1&quot;),
      JsonSubTypes.Type(Company2Data::class, name = &quot;Company2&quot;)
  ])
  abstract class EventData {
      abstract fun validate()
  }
  
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = &quot;event_type&quot;, include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
  @JsonSubTypes(value = [
      JsonSubTypes.Type(Cmp1RegisterData::class, name = &quot;REGISTER&quot;)
  ])
  abstract class Company1Data
  
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = &quot;event_type&quot;, include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
  @JsonSubTypes(value = [
      JsonSubTypes.Type(Cmp2RegisterData::class, name = &quot;REGISTER&quot;),
      JsonSubTypes.Type(DeleteData::class, name = &quot;DELETE&quot;)
  ])
  abstract class Company2Data
  
  data class Cmp1RegisterData(
      val name: String,
      val age: Int
  ) :Company1Data()
  
  data class Cmp2RegisterData(
      val name: String,
      val age: Int,
      val lastName: String
  ) :Company2Data()
  
  data class DeleteData(
      val id: String,
      val reason: String
  ) :Company2Data()
  </code></pre>
  <p>The above code does not work since both <code>CompanyData</code> are abstract, so they can not be initialized, and using class won't work since I have the <code>validate</code> function in the &quot;main&quot; abstract class and I want to override it in the last class I'm deserializing (i.e.: Cmp2RegisterData)</p>