title: Kotlin Serialization to JSON: How to ignore actual field type
body: <p>I am new to Kotlin and even newer to <a href="https://github.com/Kotlin/kotlinx.serialization" rel="nofollow noreferrer">Kotlin Serialization</a> and I am trying to convert a Kotlin data class to JSON. Look at the following sample code:</p>
  
  <pre><code>import kotlinx.serialization.Serializable
  import kotlinx.serialization.json.JSON
  
  fun main(args: Array&lt;String&gt;) {
      println(JSON.stringify(BaseClass1("abc")))
      println(JSON.stringify(BaseClass2("abc")))
  }
  
  @Serializable
  data class BaseClass1(
          val a: String
  )
  
  @Serializable
  data class BaseClass2(
          val a: Any
  )
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>{"a":"abc"}
  {"a":["kotlin.String","abc"]}
  </code></pre>
  
  <p>So I save a String in a variable of the type <code>Any</code> (which should be perfectly valid). However, this results in the serialized value being an array of the type and the value. This also happens with other classes and subclasses (when I extend the field's type for example).</p>
  
  <p>How can I get <code>stringify</code> to always use the value, regardless of the field's actual type? I want the second <code>stringify</code> result of the program above to match the first one.</p>