title: Pass DataType of the Class
body: <p>I have the following data class, which stores two values, JSON and dataType:</p>
  
  <pre><code>data class DataTypeItem(
      var json     : String = "",
      var dataType : Class&lt;*&gt; ?= null
  )
  </code></pre>
  
  <p>I have the list defined in the following way:</p>
  
  <pre><code>val dataTypeList = mutableMapOf&lt;String, DataTypeItem&gt;()
  
  dataTypeList.put( "item_1", DataTypeItem( json1, MyDataType::class.java ) )
  dataTypeList.put( "item_2", DataTypeItem( json1, List&lt;MyDataType&gt;::class.java ) )
  </code></pre>
  
  <p>Please note that in one case I'm using the MyDataType as the DataType and in the other List &lt; MyDataType >.</p>
  
  <p>Now I would like to loop through each of the dataTypeList items and parse JSON for the given data type into it's model:</p>
  
  <pre><code>fun init()
  {
      dataTypeList.forEach {
          dataTypeItem -&gt;
              val model = Gson().fromJson( dataTypeItem.value.json, dataTypeItem.value.dataType::class.java )
      }
  }
  </code></pre>
  
  <p>I'm using the following model:</p>
  
  <pre><code>data class dataTypeItem(
      @SerializedName("sqlId")
      val sqlId: String,
      @SerializedName("name")
      val name: String
  )
  </code></pre>
  
  <p>But I keep getting an Runtime exception:</p>
  
  <pre><code>Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?
  </code></pre>
  
  <p>In addition, in case it's a list, I need to call toList() on Gson().fromJSON(..):</p>
  
  <pre><code>fun init()
  {
      dataTypeList.forEach {
          dataTypeItem -&gt;
              val model;
  
              if( dataTypeItem.value.dataType::class.java is Array )
                  model = Gson().fromJson( dataTypeItem.value.json, dataTypeItem.value.dataType::class.java ).toList()
              else
                  model = Gson().fromJson( dataTypeItem.value.json, dataTypeItem.value.dataType::class.java )
      }
  }
  </code></pre>
  
  <p>How can I pass the dataType dynamically and distinguish if it's a List/Array or straight up class? In addition, whenever I try to call toList(), I get an error that it's undefined.</p>
  
  <p><strong>If I specify the class directly, then it's working fine</strong></p>
  
  <pre><code> var model = Gson().fromJson( json, DataTypeItem::class.java )
  </code></pre>
  
  <p>or</p>
  
  <pre><code> var model = Gson().fromJson( json, Array&lt;DataTypeItem&gt;::class.java )
  </code></pre>
  
  <p><strong>but I need to be able to specify it dynamically as an argument</strong></p>