Serializable

This is an official annotation in kotlin , but when adding the kmp plugin it will add extended functionality to it, mainly json converters

Json converters

To avoid creating toJson and fromJson for each serializable data class, the plugin will create extended functions for the annotated data class that do just that !

  • toJson() Converts from object into string json object
  • toJson(array) Converts from array into string json array
  • toJsonPretty() Converts from object into formatted string json object
  • fromJson() Converts string json object into data class object
  • fromJsonArray() Converts string json array into array of data class object

The plugin is aware of @JsExport annotation and it will create the js function name accordingly

Usage

Make sure you’ve setup the plugin first

Then use the annotation on your data class

@Serializable
@JsExport
data class User(val id: String, val name: String, val age: Int)

Now you can use the extension functions to convert to json

val user = User("1", "foo", 20)
val users = listOf(user,user)

println(user.toJson())
println(user.toJsonPretty())
println(User.toJson(users.toTypedArray()))

output

{"id":"1","name":"foo","age":20}
{
  "id": "1",
  "name": "foo",
  "age": 20
}
[{"id":"1","name":"foo","age":20},{"id":"1","name":"foo","age":20}]

And create objects from json

val user = User.fromJson("{\"id\":\"1\",\"name\":\"foo\",\"age\":20}")

val users = User.fromJsonArray("[{\"id\":\"1\",\"name\":\"foo\",\"age\":20},{\"id\":\"1\",\"name\":\"foo\",\"age\":20}]")

Disable Json converters

Module Level

if you don’t want the json converters to be added to the entire module, add the following in the module’s build.gradle.kts

teleresoKmp {
    disableJsonConverters = true
}

Class Level

If you want to disable for just one class

import io.telereso.kmp.annotations.SkipJsonConverters

@Serializable
@JsExport
@SkipJsonConverters
data class User(val id: String, val name: String, val age: Int)