Json
JsonFeature can be used to serialize/deserialize JSON data when sending requests and receiving responses. This functionality is provided for JVM by using the Gson
/Jackson
libraries and for Kotlin Multiplatform by using kotlinx.serialization
.
Add dependencies
Before installing JsonFeature
, you need to add a dependency for the desired serializer. If your project targets only JVM, you can add Gson or Jackson dependency. For multiplatform projects, use the kotlinx.serialization library. Depending on the included artifacts, Ktor chooses a default serializer automatically. If required, you can specify the serializer explicitly and configure it.
kotlinx.serialization (Multiplatform)
For multiplatform projects, you can use the kotlinx.serialization
library. You can add it to the project as follows:
Add the Kotlin serialization plugin, as described in the Setup section.
Add the
ktor-client-serialization
dependency:implementation "io.ktor:ktor-client-serialization:$ktor_version"implementation("io.ktor:ktor-client-serialization:$ktor_version")<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-serialization</artifactId> <version>${ktor_version}</version> </dependency>
Gson and Jackson (JVM)
To use Gson, add the following artifact to the build script:
For Jackson, add the following dependency:
Install JsonFeature
To install JsonFeature
, pass it to the install
function inside a client configuration block:
Now you can configure the required JSON serializer.
Configure a serializer
Depending on the included artifacts, Ktor chooses a default serializer automatically. You can get this serializer by calling the io.ktor.client.features.json.defaultSerializer function.
To specify the required serializer explicitly, use the serializer
property:
For the selected serializer, you can access its API and adjust a configuration. Let's see how to do this.
kotlinx.serialization
To use kotlinx.serialization, assign the KotlinxSerializer
instance to the serializer
property:
Inside the KotlinxSerializer
constructor, you can access the JsonBuilder API, for example:
You can find the full example here: client-json-kotlinx.
Gson
To use Gson, assign the GsonSerializer
instance to the serializer
property:
Inside the GsonSerializer
constructor, you can access GsonBuilder API, for example:
Jackson
To use Jackson, assign the JacksonSerializer
instance to the serializer
property:
Inside the JacksonSerializer
constructor, you can access the ObjectMapper API...
... or pass the ObjectMapper
instance directly to the JacksonSerializer
constructor.
Receive and send data
Create a data class
To receive and send data, you need to have a data class, for example:
If you use kotlinx.serialization, make sure that this class has the @Serializable
annotation:
To learn more about kotlinx.serialization
, see the Kotlin Serialization Guide.
Send data
To send a class instance within a request body as JSON, assign this instance to the body
property and set the content type to application/json
by calling contentType
:
Receive data
When a server sends a response with the application/json
content, you can deserialize it by specifying a data class as a parameter of the required request method, for example:
You can find the full example here: client-json-kotlinx.