question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to configure KotlinSerializationJsonDecoder

See original GitHub issue

It seems there isn’t a way to provide a custom Json or set config on Json for the KotlinSerializationJsonDecoder

Spring Framework 5.3.14

Workaround:

@Configuration(proxyBeanMethods = false)
class InitConfiguration {

    @ExperimentalSerializationApi
    @Bean
    fun kotlinSerializationJsonDecoder() = KotlinSerializationJsonDecoder(Json {
        explicitNulls = false
    })

}

@ExperimentalSerializationApi
@Configuration
class WebConfig(val decoder: KotlinSerializationJsonDecoder) : WebFluxConfigurer {
    override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
        super.configureHttpMessageCodecs(configurer)
        configurer.defaultCodecs().kotlinSerializationJsonDecoder(decoder)
    }
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jameswardcommented, Jan 15, 2022

I think you are right about the unnecessary bean but I guess I was hoping for something like Jackson2ObjectMapperBuilderCustomizer which provides a nice way to customize Jackson.

0reactions
albertocavalcantecommented, Aug 17, 2022

I just realized that for what I wanted to do I had to take a different approach. I wanted to configure the kotlinx.serialization Decoder for WebClient.

I ended up with something close to this:

object KSerializationConfig {
    val json = Json {
        ignoreUnknownKeys = true
        isLenient = true
        allowSpecialFloatingPointValues = true
        useArrayPolymorphism = true
        encodeDefaults = true
        explicitNulls = false
    }
}
    @Bean
    fun apiClient() = WebClient.builder()
            .baseUrl("${apiProperties.baseUrl}/rest/api")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .codecs {
                val decoder = KotlinSerializationJsonDecoder(KSerializationConfig.json)
                it.defaultCodecs().kotlinSerializationJsonDecoder(decoder)
            }.build()

Based on: https://stackoverflow.com/a/66020393/12249394 and https://discuss.kotlinlang.org/t/kotlinx-serialization-with-spring-boot/16540/15

Read more comments on GitHub >

github_iconTop Results From Across the Web

ktor with kotlinx serialization: how to use JSON.nonstrict
You can specify json configurations using the Json builder, which you pass into the KotlinxSerializer.
Read more >
KotlinSerializationJsonDecoder (Spring Framework 6.0.2 API)
Decode a byte stream into JSON and convert to Object's with kotlinx.serialization. This decoder can be used to bind @Serializable Kotlin classes, ...
Read more >
Kotlin Serialization with Spring 5.3 - Vivid Code
Use Serializers Module. If you are using your own serializers module, you need to create your own KotlinSerializationJsonHttpMessageConverter ...
Read more >
Hi I have a spring boot application and I m trying to use ko
I think you need to configure KotlinX serialization both for inbound requests ... Json) = KotlinSerializationJsonDecoder(json) @Bean fun exchangeStrategies( ...
Read more >
KotlinSerializationJsonDecoder (spring-web 5.3.10 API) - javadoc.io
Decode a byte stream into JSON and convert to Object's with kotlinx.serialization. This decoder can be used to bind @Serializable Kotlin classes, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found