Skip to content

Commit

Permalink
fix(serialization): polymorphic serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DebitCardz committed Jul 8, 2024
1 parent d38d1cc commit f4d9c0a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "gg.ingot"
version = "1.3.2"
version = "1.3.3"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.ingot.iron.serialization

import kotlinx.serialization.Serializable
import kotlinx.serialization.serializerOrNull

/**
Expand Down Expand Up @@ -32,7 +33,13 @@ interface SerializationAdapter {
}

override fun serialize(obj: Any, clazz: Class<*>): String {
val serializer = json.serializersModule.serializerOrNull(clazz)
// For polymorphic serialization we need to use the base class that'll
// include the "type" field in the JSON output, so we can then properly
// deserialize the value, so we literally have to check if the super class
// is serializable and use that instead of the actual class. - tech
val serializerClazz = clazz.superclass.takeIf { it.annotations.any { a -> a is Serializable } }
?: clazz
val serializer = json.serializersModule.serializerOrNull(serializerClazz)
?: error("No serializer found for type: ${clazz.simpleName}")

return json.encodeToString(serializer, obj)
Expand Down

0 comments on commit f4d9c0a

Please sign in to comment.