Search code examples
kotlinx.serialization

For serial name Duration there already exist DurationSerializer / Serializer has not been found for type 'Duration'


Up until now, I had been using the following libraries:

kotlin("jvm")
kotlin("plugin.serialization") version "1.7.10"    
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")

Since until now, there is no serializer for the type kotlin.time.Duration, I had my own custom serializer. So far so good.

Now I want to update the kotlinx serialization libraries to 1.4.0, which has a DurationSerializer. When I do this, the compiler naturally protests the following:

Caused by: java.lang.IllegalArgumentException: The name of serial descriptor should uniquely identify associated serializer. For serial name Duration there already exist DurationSerializer.

So I delete my custom DurationSerializer in order to use the new one included in the library, clean and attempt to recompile. However, when I do that, the following happens:

Serializer has not been found for type 'Duration'. To use context serializer as fallback, explicitly annotate type or property with @Contextual

Basically, the new library version can tell that my custome DurationSerializer is conflicting with its own, but then can't actually use its own. How do I get around this?


Solution

  • Got the answer from the kotlinx.serialization github repository here.

    Turns out the library version of the serializer will only be added in a future compiler update.

    In the meantime, our serial name for the custom serializer was too similar to the libraries', causing a conflict. Changed it from kotlin.time.Duration to CustomDurationSerializer and the issue went away.