制作GSON&境界很好玩

时间:2014-10-08 15:03:08

标签: android gson realm

我正在尝试使用Realm + GSON。 如果他们能够很好地合作,那将是天堂般的匹配。

但是,当我使用“extends 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

有什么问题吗? : - )

1 个答案:

答案 0 :(得分:26)

你可以这样做:

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