我得到一个StackOverflow
错误任何时候我尝试Serialize
一个JSON String
我POJO Object
具有两个Constructors
GSON StackOverflow的错误的Android
public class ObjectClass extends BaseObservable implements Serializable {
@Inject Realm realm;
private String TAG = "ObjectClass";
private String name;
private String address;
public ObjectClass() {
}
public ObjectClass(Context context) {
((MainApplication) context.getApplicationContext()).getMainAppComponent().inject(this);
}
public String getName() {
return name;
}
public void setname(String name) {
this.name = name;
}
@Bindable
public String getAddress() {
return address;
}
public void setaddress(String address) {
this.address = address;
Realmaddress result = realm.where(Realmaddress.class).equalTo("id", address).findFirst();
if (result != null)
Log.e(TAG, result.toString());
}
}
我应该删除这个构造
public ObjectClass(Context context) {
((MainApplication) context.getApplicationContext()).getMainAppComponent().inject(this);
}
一切工作正常。但是,如果我离开它,我会遇到StackOverFlow错误。
String testJson = "{\n" +
"\t\"name\" : \"Test Name\",\n" +
"\t\"address\" : \"Test Address\"\n" +
"}"
Gson gson = new GsonBuilder().setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'").setPrettyPrinting().create();
ObjectClass objectClass = gson.fromJson(testJson, ObjectClass.class);
一切都只是挂起,一段时间后,我收到了StackOverflow
错误
编辑1
所以,深调试完毕后。我注意到构造函数不是问题。这是@Inject Realm realm;
所以,我试图做到这一点@Expose(deserialize = false, serialize = false) @Inject Realm realm;
这也没有解决它。
EDIT 2
我编辑我GSON Exclusion Strategy
到这个样子。但是,进入后也不会解决它,如果块和日志,它与您使用dependency injection
存在
public class CustomGsonParser {
public static Gson returnCustomParser(){
return new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
if (f.getAnnotation(Inject.class) != null ||
f.getDeclaringClass().equals(RealmObject.class) ||
f.getDeclaringClass().equals(Realm.class)) {
Log.e("GSONBuild", "Exists Inject");
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
if (clazz.equals(Inject.class))
return true;
return false;
}
})
.create();
}
}
嗯,它正在使用。你是什么意思?我需要'realm',因为Object正被'databinder'使用。我可以让'Gson'省略而不关心它吗? –
@ItuokeAjanlekoko otherway从这个意义上说,您可以使用接口在这种情况下传递/发送您的数据 – Nikhil