Serializing singleton-objects using Gson


I try to use Gson to serialize a kotlin-singleton.

For some reason it’s direct members (val x: String = "") are serialized and deserialized correctly, but nested members, like val x: OtherType are skipped.

When I change the declaration from object MyClass to regular class MyClass it works as intended.

Can anyone point me in a direction here?


My mistake: The “direct members” where actually in an abstract class, extended by the object, so that’s why they were serialized.

I initially thought that kotlin creates an “instance” field for the singleton, but since they’re just static fields, they work when configuring Gson to include static fields. (gsonBuilder.excludeFieldsWithModifiers(Modifier.TRANSIENT)

Sorry for being off-topic.