Given a `kotlinx.serialization.json.Json` object. ...
# serialization
s
Given a
kotlinx.serialization.json.Json
object. Can I ask it if it is configured to serialize a given
KClass
or if it will throw an exception when encountering instances of that type ?
v
Do you know whether
KClass
is marked with
@Serializable
? What platform is it? On JVM, you can check if class has
@Serializable
annotation via reflection or if
Json.serializersModule.getContextual(KClass) != null
On multiplatform you’ll need an actual
T
for that. Then you can use something like
Json.serializersModule.serializer<T>()
to see if that returns a value or throws
s
I am on the JVM. I wanted to check if a type was serializable so I could get close to the compile time guarantees of
@Serializable
but runtime checked (in a test) when using external serializers (
@Serializer(forClass = ...)
). It is shelved for now but I guess
module.serializer<T>()
is the way to go.