I am trying to serialize certain custom data types. For the simpler ones (like UUID) the approach described in Kotlin Serialization worked great. However I am running into some issues with more complex types. Here is my code:
object class:
@Serializable(with = PairSerializer::class) //error here
private val equipment = mutableListOf<Pair<ItemSlot, ItemStack>>()
serializer class:
class PairSerializer() : KSerializer<Pair<EnumWrappers.ItemSlot, ItemStack>> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("PairEnumISSerializer") {
}
override fun serialize(encoder: Encoder, value: Pair<EnumWrappers.ItemSlot, ItemStack>) {
//implementation
}
override fun deserialize(decoder: Decoder): Pair<EnumWrappers.ItemSlot, ItemStack> {
//implementation
}
}
here is the error I am getting:
Serializer has not been found for type 'Pair<EnumWrappers.ItemSlot, ItemStack>'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
I can't exactly put
@Serializer(forClass = Pair<EnumWrappers.ItemSlot, ItemStack>::class)
above the serializer class because that shows error: "Only classes are allowed on the left hand side of a class literal" and I haven't been able to find any answers online so far.
Serializer has not been found for type 'ItemStack'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
Update: After some trial and error, this is what finally worked:
@Serializable(with = PairSerializer::class)
private val equipment = hashMapOf<ItemSlot,@Serializable(with = ItemStackSerializer::class) ItemStack>()
Essentially, both the list/map need their own serializer as does the custom data type itself