I'm trying to initialize Ktor http client and setup json serialization. I need to allow non-strict deserialization which JSON.nonstrict object allows. Just can't get how to apply this setting to serializer.
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
}
After Kotlin 1.4.0 released:
use this for converting string to Object:
val response = Json {
ignoreUnknownKeys = true
}.decodeFromString(ResponseObject.serializer(), jsonString)
And for your httpClient use:
HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer()
}
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
}
}
You can specify json configurations using the Json builder, which you pass into the KotlinxSerializer.
val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(Json {
isLenient = true
ignoreUnknownKeys = true
})
}
}
The exact fields for the Json builder is experimental and subject to change, so check out the source code here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With