RealmObjects can be read from json nowadays, but when i try to turn a realm object into json via gson it only contains primitive types and no elements or child elements which inherit from object. How would i go about converting a realm object to json so that i can later read it back into a realm with the Realm.create*FromJson methods?
Hi @mpost
Those values you see in the JSON are just default values, ie. 0 or false, and not the actual values. You will also notice that strings are missing.
GSON works by inspecting fields when serializing objects. This conflicts pretty hard with Realms design where the objects just contains pointers to the data in the database. For GSON to be able to serialize Realm objects it would need to use the proper getters and setters. You can read some discussion that topic here: http://stackoverflow.com/questions/6203487/why-does-gson-use-fields-and-not-getters-setters
The conclusion is basically that it is not going to happen and if you want to use GSON you will have to write your own serializer. You can get some inspiration on how to do that here: http://www.studytrails.com/java/json/java-google-json-custom-serializer-deserializer.jsp
That said, Realm core does have some JSON export capability, but it has not been exposed yet as it is still lacking a lot of features. So at some point we will probably provide support for serializing Realm Objects, but for now you will have to your own GSON serializer.
Just commenting to register my interest in this feature. Writing my own serializer does not look too taxing but it would be great if this feature was built in. I am building a recipe app of sorts and want to make it easy for my users to export their recipes, a simple json export would help facilitate this
@cmelchior Thanks for your elaborate answer. I follow your reasoning but hope to see a json export in the very near future. So for now i have resorted to manual conversion via gson.
I am developing an app for android wear and run realm on wear and on the phone and would like to sync some data between them via the wear api.
Small update on the idea of serialization with gson: Creating a normal JsonSerializer and registering it as a type adapter is not straight forward. Since you are actually working against the Proxy object, gson tries to match the adapter against the proxy which is not found. So you either register the serializer against the proxy or you use a different library.
@cmelchior I hope you will build up the module to play nice with GSON. Still I have alot of writen application using GSON to convert string into the object if the extended data module doesnt support gson then it will be have a big waste from using gson efforts. Keep up with the good work btw.
@jjhesk Realm should support GSON fully when importing data, if you provide a proper Gson instance as described here http://realm.io/docs/java/0.78.0/#gson. From our initial investigations it looks like it will not be possible to use Gson to convert back to JSON for the reasons described above.
We will provide some capability for that going forward, but for now you will probably have to write your own serializing code.
We will look into how this can be accomplished as part of the V2 of your JSON API. I have added a note about it here: https://github.com/realm/realm-java/issues/682
public String composeJsonFromRealm(){
ArrayList
wordList = new ArrayList
// Build the query looking at all users:
// Execute the query:
RealmResults
if (users.size()>0){
User user = users.first();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", user.getName());
map.put("email",user.getEmail());
wordList.add(map);
}
Gson gson = new GsonBuilder().create();
return gson.toJson(wordList);
}
You can now simply use Realm.copyFromRealm and then serialize it into JSON
Thanks mayne
On Mon, Dec 21, 2015 at 4:53 PM, Saket Narayan [email protected]
wrote:
You can now simply use Realm.copyFromRealm and then serialize it into JSON
—
Reply to this email directly or view it on GitHub
https://github.com/realm/realm-java/issues/812#issuecomment-166310168.
is ther any method to convert RealmObject to Gson? please tell me ,ths!
@David-Kuper
gson.toJson(realm.copyFromRealm(realmObject));
@Zhuinden Looks like pretty easy solution. Is it much more resource intensive than json serializer customization?
@brucemax if you have RealmList<> fields then you should probably specify a depth parameter to copyFromRealm like 1 or 2, I think it's a cleaner solution than specifying a json serializer considering that will also just call getters on the object anyway except the mapping would be manual
Realm already generates this in copyFromRealm()
Most helpful comment
@David-Kuper
gson.toJson(realm.copyFromRealm(realmObject));