2021-02-17 15:03:38 +00:00
|
|
|
package dev.inmo.plagubot
|
|
|
|
|
2022-05-13 07:04:49 +00:00
|
|
|
import kotlinx.serialization.KSerializer
|
|
|
|
import kotlinx.serialization.Serializer
|
|
|
|
import kotlinx.serialization.builtins.serializer
|
2021-02-17 15:03:38 +00:00
|
|
|
import kotlinx.serialization.descriptors.SerialDescriptor
|
|
|
|
import kotlinx.serialization.encoding.Decoder
|
|
|
|
import kotlinx.serialization.encoding.Encoder
|
|
|
|
|
|
|
|
@Serializer(Plugin::class)
|
2022-05-13 07:04:49 +00:00
|
|
|
class PluginSerializer : KSerializer<Plugin> {
|
|
|
|
override val descriptor: SerialDescriptor
|
|
|
|
get() = String.serializer().descriptor
|
2021-02-17 15:03:38 +00:00
|
|
|
|
|
|
|
override fun deserialize(decoder: Decoder): Plugin {
|
2022-06-20 14:52:42 +00:00
|
|
|
val kclass = Class.forName(decoder.decodeString()).kotlin
|
|
|
|
return (kclass.objectInstance ?: kclass.constructors.first { it.parameters.isEmpty() }.call()) as Plugin
|
2021-02-17 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun serialize(encoder: Encoder, value: Plugin) {
|
2022-05-13 07:04:49 +00:00
|
|
|
encoder.encodeString(
|
|
|
|
value::class.java.canonicalName
|
|
|
|
)
|
2021-02-17 15:03:38 +00:00
|
|
|
}
|
2022-01-02 06:41:15 +00:00
|
|
|
}
|