Stylianos Gakis
06/13/2024, 10:28 AMSerialDescriptor
to get the right KSerializer<>
. Or from a KSerializer
, to grab the KSerializer
of all the fields of that class.
Say I got
@Serializable
data class SomeSerializableDataClass(
val firstNullableDate: LocalDate?,
val secondNonNullDate: LocalDate,
val someRandomField: String?,
val aListOfSomethingElse: List<Foo>,
)
And I got passed inside an inline function, the reified type T
which is SomeSerializableDataClass
here so I can do serializer<T>
for it and get back KSerializer<SomeSerializableDataClass>
.
Then inside there I'd like to in some way to know that I got KSerializer<LocalDate?>
, KSerializer<LocalDate>
, KSerializer<String?>
, KSerializer<List<Foo>>
, or even just get their real types so I can call serializer<>()
on them?Stylianos Gakis
06/13/2024, 10:35 AM@Serializable
data class DateOfOccurrencePlusLocation(
val dateOfOccurrence: LocalDate?,
val maxDate: LocalDate,
val selectedLocation: String?,
val locationOptions: List<LocationOption>,
) : ClaimFlowDestination {
companion object {
val typeMap = mapOf(
typePairOfNullable<LocalDate>(),
typePairOf<LocalDate>(),
typePairOf<List<LocationOption>>(),
)
}
}
But I'd optimally like to be able to just do
@Serializable
data class DateOfOccurrencePlusLocation(
val dateOfOccurrence: LocalDate?,
val maxDate: LocalDate,
val selectedLocation: String?,
val locationOptions: List<LocationOption>,
) : ClaimFlowDestination {
companion object {
val typeMap = typeMapFor<DateOfOccurrencePlusLocation>()
}
}
And this typeMapFor
I am hoping could potentially grab the right types using the metadata given by the serialization plugin and then I could hopefully do something along the lines of
for (kclass in descriptor.elementsKclassList) {
if (isNullable) {
typePairOfNullable(kclass)
} else {
typePairOf(kclass)
}
}
But from a SerialDescriptor I am not finding a way to grab the serializer of it, or the type of it.Stylianos Gakis
06/13/2024, 10:37 AMpublic val SerialDescriptor.capturedKClass: KClass<*>?
but I was wrong. The docs say "This property is null for descriptors that are not of SerialKind. CONTEXTUAL or PolymorphicKind. OPEN kinds"Stylianos Gakis
06/13/2024, 11:20 AMephemient
06/13/2024, 4:26 PMStylianos Gakis
06/13/2024, 4:36 PMephemient
06/13/2024, 4:42 PMStylianos Gakis
06/13/2024, 5:06 PM