Serialization - Kotlin SDK
Serialization methods used by libraries that depend on reflection, such as GSON do not work with the SDK by default.
This is because the SDK compiler plugin injects a hidden field
into object models, prefixed with io_realm_kotlin_
. The SDK uses
this hidden field to manage internal object state. Any library that
relies on fields instead of getters and setters needs to ignore this
hidden field.
To use the SDK with external libraries such as GSON, exclude the hidden fields from serialization using a prefix match:
var gson: Gson = GsonBuilder() .setExclusionStrategies(object: ExclusionStrategy { override fun shouldSkipField(f: FieldAttributes?): Boolean = f?.name?.startsWith("io_realm_kotlin_") ?: false override fun shouldSkipClass(clazz: Class<*>?): Boolean = false }) .create()