title: Kotlin Gson Deserializing
body: <p>I am receiving a JSON data model that has a map wrapper <em>Table</em>. I'm trying to use generics to pass in the type that is beyond the wrapper but it's not translating well at runtime. Here's an example of my JSON file:</p>
  
  <pre><code>{
  "Table": [
   {
     "paymentmethod_id": 1,
     "paymentmethod_description": "Cash",
     "paymentmethod_code": "Cash",
     "paymentmethod_is_ach_onfile": false,
     "paymentmethod_is_element": false,
     "paymentmethod_is_reward": false,
     "paymentmethod_is_openedgeswipe": false,
     "paymentmethod_update_user_id": 1,
     "paymentmethod_insert_user_id": 1,
     "paymentmethod_insertdate": "2014-10-07 14:53:16",
     "paymentmethod_deleted": false,
     "paymentmethod_is_mobile_visible": true
     }
    ]
  }
  </code></pre>
  
  <p>The wrapper class I'm using is called Table.</p>
  
  <pre><code>data class Table&lt;T&gt;(
      @SerializedName("Table") val models : Array&lt;T&gt;
  )
  </code></pre>
  
  <p>The actual model class is PaymentMethod.</p>
  
  <pre><code>data class PaymentMethod(
      @SerializedName("paymentmethod_id") val idNumber : Int = -1
  )
  </code></pre>
  
  <p>I have created a generic data manager class that takes &lt; T > type. I think use subclasses of the data manager to localize the input and results (such as declaring the model class <em>PaymentMethod</em>.</p>
  
  <pre><code>open class NXDataManager&lt;T&gt;(manager: NXNetworkManager? = null, rpc : String?, parameters: List&lt;Pair&lt;String, String&gt;&gt;? = null, method : String = "get")
  {
     ...
  
  
  open fun sendRequest(completionHandler: (models:Array&lt;T&gt;) -&gt; Unit, errorHandler: (error:FuelError) -&gt; Unit) {
  
      val request = NXNetworkRequest(rpc, parameters, method)
  
      request.send(manager, completionHandler = { s: String -&gt;
  
          val table: Table&lt;T&gt; = Gson().fromJson(s)
  
          completionHandler(table.models)
  
      }, errorHandler = errorHandler)
  }
  
  inline fun &lt;reified T&gt; Gson.fromJson(json: String) = this.fromJson&lt;T&gt;(json, object: TypeToken&lt;T&gt;() {}.type)
  
  }
  </code></pre>
  
  <p>My subclassed data manager specifies the model to parse into.</p>
  
  <pre><code>final public class PaymentMethodsDataManager : NXDataManager&lt;PaymentMethod&gt;
  {
     constructor () : super("genGetPaymentMethods")
  
  }
  </code></pre>
  
  <p>When I run the code as:</p>
  
  <pre><code>val table: Table&lt;T&gt; = Gson().fromJson(s)
  </code></pre>
  
  <p>I get an error message <strong>java.lang.ClassCastException: java.lang.Object[] cannot be cast to Networking.PaymentMethod[]</strong>. However, when I pass in an explicit type it works as expected--parsing the array into PaymentMethod models:</p>
  
  <pre><code>val table: Table&lt;PaymentMethod&gt; = Gson().fromJson(s)
  </code></pre>
  
  <p>Any ideas of how I can still use the generic type T?</p>