Chuck Stein
03/09/2023, 5:31 PM@Serializable
. They each have many properties which are all primitives or collections of primitives, so each property should be serializable by default, with no customized serialization logic necessary. How can I make these classes serializable using the built-in serializers for their properties, rather than tediously specifying a custom serializer for each one which manually goes through the dozens of properties?Adam S
03/09/2023, 7:12 PM@Serializer(forClass = ClassFromAnotherProject::class)
https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#deriving-external-serializer-for-another-kotlin-class-experimentalChuck Stein
03/09/2023, 10:39 PM@Serializer
doesn't seem to work. Is the only option in my case then to write a custom serializer for each one that encodes each field individually?Chuck Stein
03/09/2023, 10:40 PMcomponent()
. Would be nice if I could refer to the original data class instead, since those should work with @Serializer
Adam S
03/09/2023, 10:50 PMAdam S
03/09/2023, 10:51 PMclass ExternalFoo(
val x: String,
val y: Int,
nonPropertyParam: String
) {
}
typealias ExternalFooSerialized = @Serializable(with = ExternalFooSerializer::class) ExternalFoo
object ExternalFooSerializer : KSerializer<ExternalFoo> {
@Serializable
class DelegateFoo(
val x: String,
val y: Int,
)
private val delegateSerializer get() = DelegateFoo.Companion.serializer()
override val descriptor: SerialDescriptor get() = delegateSerializer.descriptor
override fun deserialize(decoder: Decoder): ExternalFoo {
val delegate = delegateSerializer.deserialize(decoder)
return ExternalFoo(
delegate.x,
delegate.y,
"blah",
)
}
override fun serialize(encoder: Encoder, value: ExternalFoo) {
val delegate = DelegateFoo(
value.x,
value.y
)
delegateSerializer.serialize(encoder, delegate)
}
}
Chuck Stein
03/10/2023, 5:15 PM@Serializer
though, considering the class in question is a data class, and in my own source code I'm able to throw @Serializable
on data classes without any extra steps. But since the data class in the library is already compiled, I guess it gets compiled down to:
public final data class MyClass public constructor(prop1: Double, prop2: DoubleArray) {
val prop1: Double = prop1
val prop2: DoubleArray = prop2
// functions for equals, hashCode, component1, component2
}
Since it's functionally the same as any other data class, and in the source code it would be defined with constructor properties (like my own data classes which I can throw @Serializer
on without any extra steps), it seems like a shortcoming of kotlinx serialization if it can't autogenerate the serializer for this one like it can with other data classes.Adam S
03/10/2023, 5:25 PM@Serializer(forClass = ClassFromAnotherProject::class)
does - it tells the KxS plugin to generate a serializer for an external class. But if that class doesn’t meet the requirements (e.g. all parameters of the class’s primary constructor be properties), then it can’t auto-generate a serializer no matter where the class is. I don’t think there’s a sensible way to automate a solution.Chuck Stein
03/10/2023, 6:10 PMAdam S
03/10/2023, 6:11 PMChuck Stein
03/10/2023, 9:20 PMdata class MyClass(
val prop1: Double,
val prop2: DoubleArray
)
which would meet the requirements.Chuck Stein
03/10/2023, 9:21 PM