//*************************When using with Retrofit please use below converter************************************* implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0") //A Retrofit 2 Converter.Factory for Kotlin serialization. val contentType = "application/json".toMediaType() val retrofit = Retrofit.Builder() .baseUrl("https://example.com/") .addConverterFactory(Json.asConverterFactory(contentType)) .build() //**************************Dependency gradle******************************* plugins { id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10' } buildscript { ext.kotlin_version = '1.6.10' repositories { mavenCentral() } dependencies { classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" } } dependencies { implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2" } /* otlinx.serialization comes with a simple but powerful pure-Kotlin API, which makes it effortless to parse JSON into type-safe Kotlin objects, and vice versa. */ @Serializable data class User(val name: String, val yearOfBirth: Int) fun main() { // Serialization (Kotlin object to JSON string) val data = User("Louis", 1901) val string = Json.encodeToString(data) println(string) // {"name":"Louis","yearOfBirth":1901} // Deserialization (JSON string to Kotlin object) val obj = Json.decodeFromString(string) println(obj) // User(name=Louis, yearOfBirth=1901) } /* Whether you’re trying to serialize a data class with default initializers for its properties, a singleton object, or trying to deserialize a generic List: kotlinx.serialization always behaves as you would expect. */ @Serializable data class Project( val name: String, val owner: Account, val group: String = "R&D" ) @Serializable data class Account(val userName: String) val moonshot = Project("Moonshot", Account("Jane")) val cleanup = Project("Cleanup", Account("Mike"), "Maintenance") fun main() { val string = Json.encodeToString(listOf(moonshot, cleanup)) println(string) // [{"name":"Moonshot","owner":{"userName":"Jane"}},{"name":"Cleanup","owner":{"userName":"Mike"},"group":"Maintenance"}] val projectCollection = Json.decodeFromString>(string) println(projectCollection) // [Project(name=Moonshot, owner=Account(userName=Jane), group=R&D), Project(name=Cleanup, owner=Account(userName=Mike), group=Maintenance)] } /* As you can see, even the type-safe deserialization of collections with generics is handled by kotlinx.serialization just as expected. This certainly isn’t the norm, */