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.
[^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.
Imagine a serializable class
Foo
, that has a@Contextual
propertybar
of typeBar
, just like this:Then imagine whatever serializer for
Bar
calledBarSerializer
. Now if you want to deserializeFoo
, you need to add theBarSerializer
toserializersModule
. Reading from a file, it should look like thisThis 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 ofdecodeFromFile
anddecodeFromString
, while it is present in the chain ofencodeToString
.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.