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 RealmObject" I get this:
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
Any fix available? :-)
Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then, toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.
GSON has been successfully used in several android apps that are in Google Play today. The benefit you get with GSON is that object mapping can save the time spent writing code.
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 : '[email protected]' }";
User user = gson.fromJson(json, User.class);
http://realm.io/docs/java/0.77.0/#gson
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With