Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KToml deserialization ignores serializersModule #282

Open
matytyma opened this issue Nov 4, 2024 · 0 comments
Open

KToml deserialization ignores serializersModule #282

matytyma opened this issue Nov 4, 2024 · 0 comments

Comments

@matytyma
Copy link

matytyma commented Nov 4, 2024

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 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.

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

Footnotes

  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.

@matytyma matytyma changed the title KToml (de)serialization ignores serializersModule KToml deserialization ignores serializersModule Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant