Kotlinx-Serialization
The kotlinx-serialization extension package for Fuel.

Installation

You can download and install fuel-kotlinx-serialization with Maven and Gradle. The kotlinx-serialization package has the following dependencies:
1
implementation 'com.github.kittinunf.fuel:fuel:<latest-version>'
2
implementation 'com.github.kittinunf.fuel:fuel-kotlinx-serialization:<latest-version>'
Copied!

Usage

1
@Serializable
2
data class HttpBinUserAgentModel(var userAgent: String = "")
3
4
Fuel.get("/user-agent")
5
.responseObject<HttpBinUserAgentModel> { _, _, result -> }
Copied!
This is by default strict and will reject unknown keys, for that you can pass a custom Json instance Json(strictMode = false) or use a built-in alternate like Json.nonstrict
1
@Serializable
2
data class HttpBinUserAgentModel(var userAgent: String = "")
3
4
Fuel.get("/user-agent")
5
.responseObject<HttpBinUserAgentModel>(json = Json.nonstrict) { _, _, result -> }
Copied!
kotlinx.serialization can not always guess the correct serialzer to use, when generics are involved for example
1
@Serializable
2
data class HttpBinUserAgentModel(var userAgent: String = "")
3
4
Fuel.get("/list/user-agent")
5
.responseObject<HttpBinUserAgentModel>(loader = HttpBinUserAgentModel.serializer().list) { _, _, result -> }
Copied!
It can be used with coroutines by using kotlinxDeserializerOf() it takes the same json and loader as parameters
1
@Serializable
2
data class HttpBinUserAgentModel(var userAgent: String = "")
3
4
Fuel.get("/user-agent")
5
.awaitResponseObject<HttpBinUserAgentModel>(kotlinxDeserializerOf()) { _, _, result -> }
Copied!
Copy link