'Kotlin + Moshi serialization of object type

I have following code in Kotlin:

sealed class ParentClass
data class ChildA(val prop: String): ParentClass()
object ChildB: ParentClass()

but when I try to serialize it into JSON with Moshi I get following Error:

Caused by: java.lang.IllegalArgumentException: Cannot serialize object declaration ChildB
Failed to serialize obj: ChildB of type: class ChildB to a map

I don't want to include full stack trace due to confidentiality, but essentially it fails on this line. I wonder if there is a way to serialize Kotlin object types into JSON and back?



Solution 1:[1]

https://github.com/ZacSweers/MoshiX/tree/main/moshi-sealed should support that use case, I believe.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Petter Måhlén