Making GSON & Realm play nice

后端 未结 1 1063
情话喂你
情话喂你 2021-01-01 19:31

I\'m trying to use Realm + GSON. If they would work together well, it would be a match made in heaven.

However, when I extend my model objects with \"extends RealmOb

相关标签:
1条回答
  • 2021-01-01 20:09

    U can do it like this:

    Gson gson = new GsonBuilder()
        .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }
    
            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .create();
    

    String json = "{ name : 'John', email : 'john@corporation.com' }"; User user = gson.fromJson(json, User.class);

    http://realm.io/docs/java/0.77.0/#gson

    0 讨论(0)
提交回复
热议问题