Hey all, I haven't had the chance to make use of k...
# serialization
n
Hey all, I haven't had the chance to make use of kotlinx.serialization yet, but from looking at the docs I'm really excited for it, and it actually reminds me a lot of the rust library serde. One of my hopes, since kotlinx.serialization is implemented via a compiler plugin was that the compiler would be able to warn you at compile-time if you try to serialize a class that isn't serializable. This would resolve one of my major gripes with libraries like GSON: I don't want to have to try to serialize something where the serialization wasn't set up correctly, only to get a cryptic error at run time. Currently, as I understand it reading the docs, this doesn't happen -- and you get a runtime exception instead. Are there any plans currently to support this kind of compile time checking in kotlinx.serialization?
r
It results in a compile-time error:
Copy code
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.21:compile (compile) on project my-project: Compilation failure
[ERROR] /Users/REDACTED/src/main/kotlin/MyModel.kt:[18,26] Serializer has not been found for type 'MyModel.NonSerializableClass'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
K 1
It also shows up as a warning in IntelliJ
e
there are reflection-based apis in kotlinx.serialization - e.g.
Json.encodeToString(value)
-versus non-reflective
Json.encodeToString(serializer(), value)
also
@Contextual
,
@Polymorphic
may fail at runtime if you didn't register the types in the serializers module
but otherwise serialization issues should be caught at compile time