You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine a serializable class Foo, that has a @Contextual property bar of type Bar, just like this:
@Serializable
classFoo(
@Contextual
valbar: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 exception1 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.
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. ↩
The text was updated successfully, but these errors were encountered:
matytyma
changed the title
KToml (de)serialization ignores serializersModule
KToml deserialization ignores serializersModuleNov 4, 2024
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 exception1 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
Footnotes
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. ↩The text was updated successfully, but these errors were encountered: