Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 599 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ktor with kotlinx serialization: how to use JSON.nonstrict

#1
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()
}
}
Reply

#2
For those who use retrofit, you might want to consider using `JsonConfiguration(strictMode = false)` on the retrofit builder.

E.g:
```
// your retrofit builder
Retrofit.Builder()
.baseUrl(url)
.client(okHttpClient)
.client(httpClient)
.addConverterFactory(
Json(JsonConfiguration(strictMode = false))
.asConverterFactory(MediaType.get("application/json")
)
)
```

Source: [issue on the kotlinx github][1]


[1]:

[To see links please register here]

Reply

#3
Figured out - we can pass in constructor:

serializer = KotlinxSerializer(Json.nonstrict)
Reply

#4
Working from Rodion Altshuler's answer above, this is what worked for me in my KMP project:

install(JsonFeature) {
serializer = KotlinxSerializer(kotlinx.serialization.json.Json(JsonConfiguration.Stable.copy(strictMode = false))).apply {
useDefaultTransformers = true
}
}
Reply

#5
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.](

[To see links please register here]

)
Reply

#6
With the "1.0.0RC" version, the use with retrofit is as follows.



Retrofit.Builder()
.client(okHttpClient)
.baseUrl(Env.BASE_URL)
.addConverterFactory(Json{
isLenient = true
ignoreUnknownKeys = true
}.asConverterFactory(MediaType.get("application/json")))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
Reply

#7
This change very often, but with Kotlin 1.4.10 and Ktor 1.4.1 you need to pass a **kotlinx** Json (be careful because there is also a `io.ktor.client.features.json.Json`, I used an import alias to distinguish them because I needed both `import kotlinx.serialization.json.Json as KotlinJson`)

val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(KotlinJson { ignoreUnknownKeys = true })
}
...
Reply

#8
This is how you can configure the JsonConfig for the Spring reactive Webclient:

```


val json = Json { ignoreUnknownKeys = true isLenient = true }

val strategies = ExchangeStrategies
.builder()
.codecs { clientDefaultCodecsConfigurer ->
run {
clientDefaultCodecsConfigurer.defaultCodecs()
.kotlinSerializationJsonDecoder(KotlinSerializationJsonDecoder(json))
clientDefaultCodecsConfigurer.defaultCodecs()
.kotlinSerializationJsonEncoder(KotlinSerializationJsonEncoder(json))

}
}.build()

return WebClient
.builder()
.exchangeStrategies(strategies)
.baseUrl(baseUrl!!)
.build()
```
Reply

#9
It seems that for 1.4.32 I have it as follows:

```kotlin
install(JsonFeature) {
serializer = KotlinxSerializer(json = kotlinx.serialization.json.Json {
isLenient = true
ignoreUnknownKeys = true
})
}
```
Reply

#10
After Kotlin **1.4.0** released:

use this for converting string to Object:
```kotlin
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
}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through