# Serializing singleton-objects using Gson

**URL:** <https://discuss.kotlinlang.org/t/serializing-singleton-objects-using-gson/1909>\
**Category:** Support\
**Created:** [August 8, 2016, 8:07pm UTC](https://discuss.kotlinlang.org/t/serializing-singleton-objects-using-gson/1909 "2016-08-08T20:07:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [August 8, 2016, 8:07pm UTC](https://discuss.kotlinlang.org/t/serializing-singleton-objects-using-gson/1909/1 "2016-08-08T20:07:52Z")

</div>

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?  
Thanks

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [August 9, 2016, 7:06am UTC](https://discuss.kotlinlang.org/t/serializing-singleton-objects-using-gson/1909/2 "2016-08-09T07:06:02Z")

</div>

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.
