Realm + Json
The Finest Artist
Realm
RealmObject
RealmObjectProxy
RealmList
RealmResult
RealmChangeListener
RealmObject
public abstract class RealmObject {
protected Row row;
protected Realm realm;
public void removeFromRealm() { /* ... */ }
public boolean isValid() { /* ... */ }
protected static Realm getRealm(RealmObject obj) { /* ... */ }
protected static Row getRow(RealmObject obj) { /* ... */ }
}
RealmObjectProxy
public interface RealmObjectProxy {
}
RealmList
public class RealmList<E extends RealmObject> extends AbstractList<E> {
private final boolean managedMode;
private Class<E> clazz;
private LinkView view;
private Realm realm;
private List<E> nonManagedList;
public RealmList() { /* ... */ }
public RealmList(E... objects) { /* ... */ }
RealmList(Class<E> clazz, LinkView view, Realm realm) { /* ... */ }
@Override public void add(int location, E object) { /* ... */ }
@Override public boolean add(E object) { /* ... */ }
@Override public E set(int location, E object) { /* ... */ }
RealmList
private E copyToRealmIfNeeded(E object) { /* ... */ }
public void move(int oldPos, int newPos) { /* ... */ }
@Override public void clear() { /* ... */ }
@Override public E remove(int location) { /* ... */ }
@Override public E get(int location) { /* ... */ }
public E first() { /* ... */ }
public E last() { /* ... */ }
@Override public int size() { /* ... */ }
public RealmQuery<E> where() { /* ... */ }
private void checkValidObject(E object) { /* ... */ }
private void checkIndex(int location) { /* ... */ }
@Override public String toString() { /* ... */ }
}
Owner
public class Owner extends RealmObject {
private String name;
private RealmList<Dog> dogs;
private Cat cat;
public Cat getCat() { /* ... */ }
public void setCat(Cat cat) { /* ... */ }
public RealmList<Dog> getDogs() { /* ... */ }
public void setDogs(RealmList<Dog> dogs) { /* ... */ }
public String getName() { /* ... */ }
public void setName(String name) { /* ... */ }
}
OwnerRealmProxy
public class OwnerRealmProxy extends Owner implements RealmObjectProxy {
private static long INDEX_NAME;
private static long INDEX_DOGS;
private static long INDEX_CAT;
private static final List<String> FIELD_NAMES;
@Override public String getName() { /* ... */ }
@Override public void setName(String value) { /* ... */ }
@Override public RealmList<Dog> getDogs() { /* ... */ }
@Override public void setDogs(RealmList<Dog> value) { /* ... */ }
@Override public Cat getCat() { /* ... */ }
@Override public void setCat(Cat value) { /* ... */ }
}
When?
realm.beginTransaction();
{
Owner owner = new Owner();
owner.setName("Leonardo");
}
realm.commitTransaction();
Owner owner = new Owner();
owner.setName("Leonardo");
realm.beginTransaction();
{
Owner ownerProxy = realm.copyToRealm(owner);
}
realm.commitTransaction();
JsonString to RealmObject
public <E extends RealmObject> void createAllFromJson(Class<E> clazz, JSONArray json) { /*...*/ }
public <E extends RealmObject> void createOrUpdateAllFromJson(Class<E> clazz, JSONArray json) { /*...*/ }
public <E extends RealmObject> void createAllFromJson(Class<E> clazz, String json) { /*...*/ }
public <E extends RealmObject> void createOrUpdateAllFromJson(Class<E> clazz, String json) { /*...*/}
public <E extends RealmObject> E createObjectFromJson(Class<E> clazz, JSONObject json) { /*...*/ }
public <E extends RealmObject> E createOrUpdateObjectFromJson(Class<E> clazz, JSONObject json) { /*…*/ }
public <E extends RealmObject> E createObjectFromJson(Class<E> clazz, String json) { /*...*/ }
public <E extends RealmObject> E createOrUpdateObjectFromJson(Class<E> clazz, String json) { /*...*/}
RealmObject to JsonString
Moshi (Square)
Gson (Google)
Jackson (FasterXML)
JsonPullParser (Vvakame)
Serialize (Cmelchior/Gist)
Rson (TheFinestArtist)

http://www.slideshare.net/TheFinestArtist/realm-51344456
RealmObject & ProxyObject
AllTypes allTypes;
Logger.d("Gson(Realm Object)");
Logger.json(gson.toJson(allTypes));
realm.beginTransaction();
AllTypes allTypesProxy = realm.copyToRealm(allTypes);
realm.commitTransaction();
Logger.d("Gson(Proxy Object)");
Logger.json(gson.toJson(allTypesProxy));
Moshi
Moshi moshi = new Moshi.Builder()
.add(new MoshiDateAdapter())
.build();
JsonAdapter<AllTypes> adapter = moshi.adapter(AllTypes.class);
// No JsonAdapter for
java.lang.Class<com.thefinestartist.realm.json.models.Dog>
annotated []
Gson
RealmObject
{
"columnBinary": [
0,
1,
2,
3,
4,
5,
6,
7
],
"columnBoolean": true,
"columnDate": "Aug 5, 2015 8:30:55 PM",
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnString": "allType"
}
ProxyObject
{
"columnBoolean": true,
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123
}
Doesn’t work for ProxyObject
Jackson
RealmObject
{
"columnBinary": "AAECAwQFBgc=",
"columnBoolean": true,
"columnDate": 1438821055494,
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnRealmList": null,
"columnRealmObject": null,
"columnString": "allType"
}
ProxyObject
{
"columnBinary": "AAECAwQFBgc=",
"columnBoolean": true,
"columnDate": 1438821055000,
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnRealmList": [],
"columnRealmObject": null,
"columnString": "allType"
}
@JsonIgnoreProperties({“valid”})
Json Pull Parser
RealmObject
{
"columnString": "allType",
"columnLong": 123,
"columnFloat": 12.3,
"columnDouble": 1.23,
"columnBoolean": true,
"columnDate": 1438821055494,
"columnRealmObject": null
}
ProxyObject
{
"columnString": "allType",
"columnLong": 123,
"columnFloat": 12.3,
"columnDouble": 1.23,
"columnBoolean": true,
"columnDate": 1438821055000,
"columnRealmObject": null
}
Doesn’t work for RealmList!!
Serialize
RealmObject
{
"columnBinary": [
0,
1,
2,
3,
4,
5,
6,
7
],
"columnBoolean": true,
"columnDate": "Aug 5, 2015 8:30:55 PM",
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnString": "allType"
}
ProxyObject
{
"columnBinary": [
0,
1,
2,
3,
4,
5,
6,
7
],
"columnBoolean": true,
"columnDate": "Aug 5, 2015 8:30:55 PM",
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnString": "allType"
}
Serialize
public class AllTypesSerializer implements JsonSerializer<AllTypes> {
@Override
public JsonElement serialize(AllTypes src, Type typeOfSrc, JsonSerializationContext context) {
final JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("columnString", src.getColumnString());
jsonObject.addProperty("columnLong", src.getColumnLong());
jsonObject.addProperty("columnFloat", src.getColumnFloat());
jsonObject.addProperty("columnDouble", src.getColumnDouble());
jsonObject.addProperty("columnBoolean", src.isColumnBoolean());
jsonObject.addProperty("columnDate", DateUtil.getDateFormat(src.getColumnDate()));
jsonObject.add("columnBinary", context.serialize(src.getColumnBinary()));
jsonObject.add("columnRealmObject", context.serialize(src.getColumnRealmObject()));
jsonObject.add("columnRealmList", context.serialize(src.getColumnRealmList()));
return jsonObject;
}
}
Rson
RealmObject
{
"columnBinary": [
0,
1,
2,
3,
4,
5,
6,
7
],
"columnBoolean": true,
"columnDate": "Aug 5, 2015 8:30:55 PM",
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnString": "allType"
}
ProxyObject
{
"columnBinary": [
0,
1,
2,
3,
4,
5,
6,
7
],
"columnBoolean": true,
"columnDate": "Aug 5, 2015 8:30:55 PM",
"columnDouble": 1.23,
"columnFloat": 12.3,
"columnLong": 123,
"columnString": "allType"
}
Rson
Rson.toJsonString(RealmObject object);
Rson.toJsonString(RealmObject object, int depth);
Royal
https://github.com/TheFinestArtist/Royal-Android
Royal
Royal
RoyalDatabase
RoyalTransaction
RoyalExport
Rson
Dependencies
dependencies {
compile 'com.thefinestartist:royal:0.82.0.6'
}
Dependencies
dependencies {
compile 'com.thefinestartist:royal:0.82.0.6'
}
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Royal.joinWith(this);
}
}
RoyalDatabase
public class SecondaryDatabase extends RoyalDatabase {
public String getFileName() { return "secondary"; }
public boolean forCache() { return false; }
public byte[] getEncryptionKey() { return null; }
public int getVersion() { return 0; }
public boolean shouldDeleteIfMigrationNeeded() { return false; }
public Set<Object> getModules() {
Set<Object> set = new HashSet<>();
set.add(new SecondaryModule());
return set;
}
@Override public long execute(Realm realm, long version) {
return getVersion();
}
}
RoyalDatabase
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Royal.joinWith(this);
Royal.addDatabase(new SecondaryDatabase());
}
}
RealmConfiguration configuration =
Royal.getConfigurationOf(SecondaryDatabase.class);
RoyalTransaction
// RealmList, RealmResults, RealmObject, List<? extends RealmObject>
RoyalTransaction.create(Class<? extends RoyalDatabase> clazz, Object... objects);
RoyalTransaction.create(RealmConfiguration configuration, Object... objects);
RoyalTransaction.create(Realm realm, Object... objects);
RoyalTransaction.save(Class<? extends RoyalDatabase> clazz, Object... objects);
RoyalTransaction.save(RealmConfiguration configuration, Object... objects);
RoyalTransaction.save(Realm realm, Object... objects);
RoyalTransaction.delete(Class<? extends RoyalDatabase> clazz, Object... objects);
RoyalTransaction.delete(RealmConfiguration configuration, Object... objects);
RoyalTransaction.delete(Realm realm, Object... objects);
RoyalTransaction
User user1 = new User();
user1.setName("Leonardo Taehwan Kim");
user1.setEmail("contact@thefinestartist.com");
user1.setUsername("TheFinestArtist");
User user2 = new User();
user2.setName("Leonardo Taehwan Kim");
user2.setEmail("contact@thefinestartist.com");
user2.setUsername("TheFinestArtist");
RoyalTransaction.save(realm, user1, user2);
RoyalTransaction.save(RealmDatabase.class, user1, user2);
RoyalExport
RoyalExport.toEmail(Class<? extends RoyalDatabase>... clazzes);
RoyalExport.toEmail(String email, Class<? extends RoyalDatabase>... clazzes);
RoyalExport.toEmail(String email, Intent intent, Class<? extends RoyalDatabase>... clazzes);
RoyalExport.toEmail(RealmConfiguration... configurations);
RoyalExport.toEmail(String email, RealmConfiguration... configurations);
RoyalExport.toEmail(String email, Intent intent, RealmConfiguration... configurations);
RoyalExport.toEmail(Realm... realms);
RoyalExport.toEmail(String email, Realm... realms);
RoyalExport.toEmail(String email, Intent intent, Realm... realms);
RoyalExport.toEmailAsRawFile();
RoyalExport.toEmailAsRawFile(String email);
RoyalExport.toEmailAsRawFile(String email, Intent intent);
RoyalExport.toExternalStorage(Class<? extends RoyalDatabase>... clazzes)
RoyalExport.toExternalStorage(RealmConfiguration... configurations);
RoyalExport.toExternalStorage(Realm... realms);
RoyalExport.toExternalStorageAsRawFile();
Rson
Rson.toJsonString(RealmObject object);
Rson.toJsonString(RealmObject object, int depth);
Thank You :)

Realm to Json & Royal

  • 1.
    Realm + Json TheFinest Artist
  • 2.
  • 3.
    RealmObject public abstract classRealmObject { protected Row row; protected Realm realm; public void removeFromRealm() { /* ... */ } public boolean isValid() { /* ... */ } protected static Realm getRealm(RealmObject obj) { /* ... */ } protected static Row getRow(RealmObject obj) { /* ... */ } }
  • 4.
  • 5.
    RealmList public class RealmList<Eextends RealmObject> extends AbstractList<E> { private final boolean managedMode; private Class<E> clazz; private LinkView view; private Realm realm; private List<E> nonManagedList; public RealmList() { /* ... */ } public RealmList(E... objects) { /* ... */ } RealmList(Class<E> clazz, LinkView view, Realm realm) { /* ... */ } @Override public void add(int location, E object) { /* ... */ } @Override public boolean add(E object) { /* ... */ } @Override public E set(int location, E object) { /* ... */ }
  • 6.
    RealmList private E copyToRealmIfNeeded(Eobject) { /* ... */ } public void move(int oldPos, int newPos) { /* ... */ } @Override public void clear() { /* ... */ } @Override public E remove(int location) { /* ... */ } @Override public E get(int location) { /* ... */ } public E first() { /* ... */ } public E last() { /* ... */ } @Override public int size() { /* ... */ } public RealmQuery<E> where() { /* ... */ } private void checkValidObject(E object) { /* ... */ } private void checkIndex(int location) { /* ... */ } @Override public String toString() { /* ... */ } }
  • 7.
    Owner public class Ownerextends RealmObject { private String name; private RealmList<Dog> dogs; private Cat cat; public Cat getCat() { /* ... */ } public void setCat(Cat cat) { /* ... */ } public RealmList<Dog> getDogs() { /* ... */ } public void setDogs(RealmList<Dog> dogs) { /* ... */ } public String getName() { /* ... */ } public void setName(String name) { /* ... */ } }
  • 8.
    OwnerRealmProxy public class OwnerRealmProxyextends Owner implements RealmObjectProxy { private static long INDEX_NAME; private static long INDEX_DOGS; private static long INDEX_CAT; private static final List<String> FIELD_NAMES; @Override public String getName() { /* ... */ } @Override public void setName(String value) { /* ... */ } @Override public RealmList<Dog> getDogs() { /* ... */ } @Override public void setDogs(RealmList<Dog> value) { /* ... */ } @Override public Cat getCat() { /* ... */ } @Override public void setCat(Cat value) { /* ... */ } }
  • 9.
    When? realm.beginTransaction(); { Owner owner =new Owner(); owner.setName("Leonardo"); } realm.commitTransaction(); Owner owner = new Owner(); owner.setName("Leonardo"); realm.beginTransaction(); { Owner ownerProxy = realm.copyToRealm(owner); } realm.commitTransaction();
  • 10.
    JsonString to RealmObject public<E extends RealmObject> void createAllFromJson(Class<E> clazz, JSONArray json) { /*...*/ } public <E extends RealmObject> void createOrUpdateAllFromJson(Class<E> clazz, JSONArray json) { /*...*/ } public <E extends RealmObject> void createAllFromJson(Class<E> clazz, String json) { /*...*/ } public <E extends RealmObject> void createOrUpdateAllFromJson(Class<E> clazz, String json) { /*...*/} public <E extends RealmObject> E createObjectFromJson(Class<E> clazz, JSONObject json) { /*...*/ } public <E extends RealmObject> E createOrUpdateObjectFromJson(Class<E> clazz, JSONObject json) { /*…*/ } public <E extends RealmObject> E createObjectFromJson(Class<E> clazz, String json) { /*...*/ } public <E extends RealmObject> E createOrUpdateObjectFromJson(Class<E> clazz, String json) { /*...*/}
  • 11.
    RealmObject to JsonString Moshi(Square) Gson (Google) Jackson (FasterXML) JsonPullParser (Vvakame) Serialize (Cmelchior/Gist) Rson (TheFinestArtist)
 http://www.slideshare.net/TheFinestArtist/realm-51344456
  • 12.
    RealmObject & ProxyObject AllTypesallTypes; Logger.d("Gson(Realm Object)"); Logger.json(gson.toJson(allTypes)); realm.beginTransaction(); AllTypes allTypesProxy = realm.copyToRealm(allTypes); realm.commitTransaction(); Logger.d("Gson(Proxy Object)"); Logger.json(gson.toJson(allTypesProxy));
  • 13.
    Moshi Moshi moshi =new Moshi.Builder() .add(new MoshiDateAdapter()) .build(); JsonAdapter<AllTypes> adapter = moshi.adapter(AllTypes.class); // No JsonAdapter for java.lang.Class<com.thefinestartist.realm.json.models.Dog> annotated []
  • 14.
    Gson RealmObject { "columnBinary": [ 0, 1, 2, 3, 4, 5, 6, 7 ], "columnBoolean": true, "columnDate":"Aug 5, 2015 8:30:55 PM", "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnString": "allType" } ProxyObject { "columnBoolean": true, "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123 } Doesn’t work for ProxyObject
  • 15.
    Jackson RealmObject { "columnBinary": "AAECAwQFBgc=", "columnBoolean": true, "columnDate":1438821055494, "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnRealmList": null, "columnRealmObject": null, "columnString": "allType" } ProxyObject { "columnBinary": "AAECAwQFBgc=", "columnBoolean": true, "columnDate": 1438821055000, "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnRealmList": [], "columnRealmObject": null, "columnString": "allType" } @JsonIgnoreProperties({“valid”})
  • 16.
    Json Pull Parser RealmObject { "columnString":"allType", "columnLong": 123, "columnFloat": 12.3, "columnDouble": 1.23, "columnBoolean": true, "columnDate": 1438821055494, "columnRealmObject": null } ProxyObject { "columnString": "allType", "columnLong": 123, "columnFloat": 12.3, "columnDouble": 1.23, "columnBoolean": true, "columnDate": 1438821055000, "columnRealmObject": null } Doesn’t work for RealmList!!
  • 17.
    Serialize RealmObject { "columnBinary": [ 0, 1, 2, 3, 4, 5, 6, 7 ], "columnBoolean": true, "columnDate":"Aug 5, 2015 8:30:55 PM", "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnString": "allType" } ProxyObject { "columnBinary": [ 0, 1, 2, 3, 4, 5, 6, 7 ], "columnBoolean": true, "columnDate": "Aug 5, 2015 8:30:55 PM", "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnString": "allType" }
  • 18.
    Serialize public class AllTypesSerializerimplements JsonSerializer<AllTypes> { @Override public JsonElement serialize(AllTypes src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("columnString", src.getColumnString()); jsonObject.addProperty("columnLong", src.getColumnLong()); jsonObject.addProperty("columnFloat", src.getColumnFloat()); jsonObject.addProperty("columnDouble", src.getColumnDouble()); jsonObject.addProperty("columnBoolean", src.isColumnBoolean()); jsonObject.addProperty("columnDate", DateUtil.getDateFormat(src.getColumnDate())); jsonObject.add("columnBinary", context.serialize(src.getColumnBinary())); jsonObject.add("columnRealmObject", context.serialize(src.getColumnRealmObject())); jsonObject.add("columnRealmList", context.serialize(src.getColumnRealmList())); return jsonObject; } }
  • 19.
    Rson RealmObject { "columnBinary": [ 0, 1, 2, 3, 4, 5, 6, 7 ], "columnBoolean": true, "columnDate":"Aug 5, 2015 8:30:55 PM", "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnString": "allType" } ProxyObject { "columnBinary": [ 0, 1, 2, 3, 4, 5, 6, 7 ], "columnBoolean": true, "columnDate": "Aug 5, 2015 8:30:55 PM", "columnDouble": 1.23, "columnFloat": 12.3, "columnLong": 123, "columnString": "allType" }
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    Dependencies dependencies { compile 'com.thefinestartist:royal:0.82.0.6' } publicclass App extends Application { @Override public void onCreate() { super.onCreate(); Royal.joinWith(this); } }
  • 25.
    RoyalDatabase public class SecondaryDatabaseextends RoyalDatabase { public String getFileName() { return "secondary"; } public boolean forCache() { return false; } public byte[] getEncryptionKey() { return null; } public int getVersion() { return 0; } public boolean shouldDeleteIfMigrationNeeded() { return false; } public Set<Object> getModules() { Set<Object> set = new HashSet<>(); set.add(new SecondaryModule()); return set; } @Override public long execute(Realm realm, long version) { return getVersion(); } }
  • 26.
    RoyalDatabase public class Appextends Application { @Override public void onCreate() { super.onCreate(); Royal.joinWith(this); Royal.addDatabase(new SecondaryDatabase()); } } RealmConfiguration configuration = Royal.getConfigurationOf(SecondaryDatabase.class);
  • 27.
    RoyalTransaction // RealmList, RealmResults,RealmObject, List<? extends RealmObject> RoyalTransaction.create(Class<? extends RoyalDatabase> clazz, Object... objects); RoyalTransaction.create(RealmConfiguration configuration, Object... objects); RoyalTransaction.create(Realm realm, Object... objects); RoyalTransaction.save(Class<? extends RoyalDatabase> clazz, Object... objects); RoyalTransaction.save(RealmConfiguration configuration, Object... objects); RoyalTransaction.save(Realm realm, Object... objects); RoyalTransaction.delete(Class<? extends RoyalDatabase> clazz, Object... objects); RoyalTransaction.delete(RealmConfiguration configuration, Object... objects); RoyalTransaction.delete(Realm realm, Object... objects);
  • 28.
    RoyalTransaction User user1 =new User(); user1.setName("Leonardo Taehwan Kim"); user1.setEmail("contact@thefinestartist.com"); user1.setUsername("TheFinestArtist"); User user2 = new User(); user2.setName("Leonardo Taehwan Kim"); user2.setEmail("contact@thefinestartist.com"); user2.setUsername("TheFinestArtist"); RoyalTransaction.save(realm, user1, user2); RoyalTransaction.save(RealmDatabase.class, user1, user2);
  • 29.
    RoyalExport RoyalExport.toEmail(Class<? extends RoyalDatabase>...clazzes); RoyalExport.toEmail(String email, Class<? extends RoyalDatabase>... clazzes); RoyalExport.toEmail(String email, Intent intent, Class<? extends RoyalDatabase>... clazzes); RoyalExport.toEmail(RealmConfiguration... configurations); RoyalExport.toEmail(String email, RealmConfiguration... configurations); RoyalExport.toEmail(String email, Intent intent, RealmConfiguration... configurations); RoyalExport.toEmail(Realm... realms); RoyalExport.toEmail(String email, Realm... realms); RoyalExport.toEmail(String email, Intent intent, Realm... realms); RoyalExport.toEmailAsRawFile(); RoyalExport.toEmailAsRawFile(String email); RoyalExport.toEmailAsRawFile(String email, Intent intent); RoyalExport.toExternalStorage(Class<? extends RoyalDatabase>... clazzes) RoyalExport.toExternalStorage(RealmConfiguration... configurations); RoyalExport.toExternalStorage(Realm... realms); RoyalExport.toExternalStorageAsRawFile();
  • 30.
  • 33.