让GSON&Realm玩起来不错

我正在尝试使用Realm + GSON。 如果他们能够很好地合作,那将是天上的一场比赛。

但是,当我扩展我的模型对象与“扩展RealmObject”我得到这个:

10-08 17:00:19.578 12492-12492/appwise.be.gsontestproject E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: appwise.be.gsontestproject, PID: 12492 java.lang.StackOverflowError at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:372) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:128) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:75) at com.google.gson.Gson.getAdapter(Gson.java:358) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:109) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:84) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:129) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:75) at com.google.gson.Gson.getAdapter(Gson.java:358) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getFieldAdapter(ReflectiveTypeAdapterFactory.java:109) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.access$100(ReflectiveTypeAdapterFactory.java:46) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:84) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:83) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:129) at com.google.gson.internal.bin 

任何修复可用? 🙂

Solutions Collecting From Web of "让GSON&Realm玩起来不错"

你可以这样做:

 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