realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RealmObject to json

Open mpost opened this issue 11 years ago • 14 comments

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?

mpost avatar Feb 02 '15 22:02 mpost

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.

cmelchior avatar Feb 03 '15 20:02 cmelchior

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

dashbad avatar Feb 03 '15 22:02 dashbad

@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.

mpost avatar Feb 04 '15 09:02 mpost

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.

mpost avatar Feb 04 '15 20:02 mpost

@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 avatar Feb 06 '15 06:02 jjhesk

@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.

cmelchior avatar Feb 06 '15 07:02 cmelchior

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

cmelchior avatar Feb 09 '15 08:02 cmelchior

public String composeJsonFromRealm(){ ArrayList<HashMap<String, String>> wordList; wordList = new ArrayList<HashMap<String, String>>(); // Build the query looking at all users: // Execute the query: RealmResults users = realm.where(User.class).findAll();

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

}

goodlifechris avatar Dec 21 '15 13:12 goodlifechris

You can now simply use Realm.copyFromRealm and then serialize it into JSON

saket avatar Dec 21 '15 13:12 saket

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.

goodlifechris avatar Jan 04 '16 08:01 goodlifechris

is ther any method to convert RealmObject to Gson? please tell me ,ths!

David-Kuper avatar Dec 19 '16 11:12 David-Kuper

@David-Kuper

gson.toJson(realm.copyFromRealm(realmObject));

Zhuinden avatar Dec 19 '16 11:12 Zhuinden

@Zhuinden Looks like pretty easy solution. Is it much more resource intensive than json serializer customization?

brucemax avatar Feb 21 '18 19:02 brucemax

@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()

Zhuinden avatar Feb 21 '18 19:02 Zhuinden