what is the json object in kotlin code example
Example 1: kotlin json
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
data class TestModel(
val id: Int,
val description: String
)
var gson = Gson()
var jsonString = gson.toJson(TestModel(1,"Test"))
var jsonString = """{"id":1,"description":"Test"}""";
var testModel = gson.fromJson(jsonString, TestModel::class.java)
Example 2: 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 )
}