Hi people, how come regardless of my `Json {}` con...
# serialization
n
Hi people, how come regardless of my
Json {}
configuration the serialisation of the following class results in only a
type
and is missing the
state
value..
Copy code
@Serializable
data object Toggle : StateAction {
    @EncodeDefault(EncodeDefault.Mode.ALWAYS)
    override val state: State = State.TOGGLE
}
If I change it to an data class with the state having the same default value it works.. What is this kind of mechanic and how to make sure it is working?
a
I would guess that the plugin-generated serializer sees that it's an object and won't bother adding fields to the descriptor. If you have a dig through the SerialDescriptor, does that seem to be the case?
Copy code
val serializer = Toggle.serializer()
val elementNames = serializer.descriptor.elementDescriptors.joinToString { it.serialName }
println("elementNames: $elementNames")
Perhaps the generated descriptor should check the properties of an
object
, to see if there are
@EncodeDefault
fields? Have you looked on the GitHub issue board to see if there's a similar issue? You could hand write a serializer, but the easiest option would be to just make
Toggle
a class!
n
Thanks for the answer Adam. While digging through the descriptor it indeed is empty and I think you’re right. Haven’t found any issues on the board. I’ve changed the state action interface into a data class an made some companion objects for quick access.. Thanks for the help
👍 1