Skip to content

TrueNight/RealmSupportForGson

Repository files navigation

RealmSupportForGson

Download Maven Central Licence

Support for serializing RealmObject with Gson

Overview

When you try to serialize RealmObject taken from database (Proxy) you will get StackOverflowError the library resolves that problem.

Installation

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>

Usage

Creating instance

  // similar to new Gson() but with RealmObject serializing support
  Gson gson = RealmSupportGsonFactory.create();
  gson.toJson(someRealmObjectProxy);

Creating instance with GsonBuilder

  // similar to new GsonBuilder().registerTypeAdapter(SomeRealmObject.class, new SomeRealmObjectAdapter()).create()
  Gson gson = RealmSupportGsonFactory.create(new GsonBuilder()
                .registerTypeAdapter(SomeRealmObject.class, new SomeRealmObjectAdapter()));
  gson.toJson(someRealmObjectProxy);

Specifying instance of Realm

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);

About

Support for serializing RealmObject with Gson

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages