orchestr7 / ktoml

Kotlin Multiplatform parser and compile-time serializer/deserializer for TOML format (Native, JS, JVM) based on KxS
https://akuleshov7.github.io/ktoml
MIT License
455 stars 25 forks source link

KToml deserialization ignores `serializersModule` #282

Open matytyma opened 3 weeks ago

matytyma commented 3 weeks ago

Imagine a serializable class Foo, that has a @Contextual property bar of type Bar, just like this:

@Serializable
class Foo(
    @Contextual
    val bar: Bar
)

Then imagine whatever serializer for Bar called BarSerializer. Now if you want to deserialize Foo, you need to add the BarSerializer to serializersModule. Reading from a file, it should look like this

val module = SerializersModule {
    contextual(BarSerializer)
}

val reader = TomlFileReader(serializersModule = module)

val foo = reader.decodeFromFile<Foo>(serializer(), "somefile.toml")

This doesn't work though - an exception[^1] will be raised. I've tried the equivalent in JSON and everything worked as expected.

When I further dug into the source code, I couldn't find any usage of serializersModule in the way of decodeFromFile and decodeFromString, while it is present in the chain of encodeToString.

Exact implementation details for my case can be found on the Kotlin Slack

[^1]: kotlinx.serialization.SerializationException: Serializer for class 'Bar' is not found. Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.