Answers for "kotlin json object with objects"

1

kotlin json

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
class MyJsonClass(val name: String, val optionalInt: Int = 5)

fun main() {
  val jsonToEncode = MyJsonClass( "Alice", 10 )
  val encodedJson  = Json.encodeToString( jsonToEncode )
  val decodedJson  = Json.decodeFromString<MyJsonClass>( jsonString )
}
Posted by: Guest on January-30-2021

Browse Popular Code Answers by Language