Skip to content

Instantly share code, notes, and snippets.

@buijs-dev
Created January 20, 2022 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buijs-dev/f7d197bce7ab44578cf7bc1c568aa6a2 to your computer and use it in GitHub Desktop.
Save buijs-dev/f7d197bce7ab44578cf7bc1c568aa6a2 to your computer and use it in GitHub Desktop.
serialize with kotlinx from superclass
//Parent
@Serializable
abstract class KlutterJSON<T>(
private val serializer: SerializationStrategy<T>,
){
//todo need reference to child class body to serialize it
fun toKJson() = Json.encodeToString(serializer, ????)
//Doesnt work obviously:
//fun toKJson() = Json.encodeToString(serializer, this)
}
//Child
@Serializable
class Foo(val bar: String): KlutterJSON<Foo>(serializer())
@Test
fun shouldTest() {
assertEquals("""{"bar":"boo"}""", Foo(bar = "boo").toKJson())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment