title: How to parse JSON objects into an enum
body: <p>I have a JSON object that is something like</p>
  
  <pre><code>{
     "tsp": "ABC" // can be only one of three things: "ABC", "DEF", "GHI"
     "userId" : "lkajsdlk-199191-lkjdflakj"
  }
  </code></pre>
  
  <p>Instead of writing a dataclass along the lines of</p>
  
  <pre><code>data class User(@SerializedName("tsp") val tsp: String, @SerializedName("userId") val userId: String
  </code></pre>
  
  <p>i'd like to have an enum that defines the three values so that my data class can be</p>
  
  <pre><code>data class User(@SerializedName("tsp") val tsp: TspEnum, @SerializedName("userId") val userId: String
  </code></pre>
  
  <p>I had tried writing an enum that was</p>
  
  <pre><code>enum class TspEnum(provider: String) {
     AY_BEE_CEE("ABC"),
     DEE_EE_EFF("DEF"),
     GEE_HAYTCH_I("GHI");
  }
  </code></pre>
  
  <p>however that did not work out</p>
  
  <p>I've realized now that calling <code>TspEnum.provider</code> will return the value of the enum, but I'm not sure how to make GSON coöperate whilst serializing the JSON into the kotlin data class</p>
  
  <p>I've read that there is an issue with Kotlin typing and GSON here: <a href="https://discuss.kotlinlang.org/t/json-enum-deserialization-breakes-kotlin-null-safety/11670" rel="nofollow noreferrer">https://discuss.kotlinlang.org/t/json-enum-deserialization-breakes-kotlin-null-safety/11670</a></p>
  
  <p>however, the way that person is serializing the hair colours to map into an enum is different enough from my tsp json object to make me scratch my head.</p>
  
  <p>Any pointers on where i'm going wrong would be great, cheers!</p>