Support for serializing RealmObject with Gson
When you try to serialize RealmObject taken from database (Proxy) you will get StackOverflowError the library resolves that problem.
Add dependency to your build.gradle file:
dependencies {
compile 'xyz.truenight.support:gson-realm:1.2.0'
}or to your pom.xml if you're using Maven:
<dependency>
<groupId>xyz.truenight.support</groupId>
<artifactId>gson-realm</artifactId>
<version>1.2.0</version>
<type>pom</type>
</dependency> // similar to new Gson() but with RealmObject serializing support
Gson gson = RealmSupportGsonFactory.create();
gson.toJson(someRealmObjectProxy); // similar to new GsonBuilder().registerTypeAdapter(SomeRealmObject.class, new SomeRealmObjectAdapter()).create()
Gson gson = RealmSupportGsonFactory.create(new GsonBuilder()
.registerTypeAdapter(SomeRealmObject.class, new SomeRealmObjectAdapter()));
gson.toJson(someRealmObjectProxy);By default library use Realm instance obtained by Realm.getDefaultInstance() but you can specify the way to get instance:
Gson gson = RealmSupportGsonFactory.create(new RealmHook() {
@Override
public Realm instance() {
return Realm.getInstance(myRealmConfiguration);
}
});
gson.toJson(someRealmObjectProxy);