Gson中的Stackoverflow异常 [英] Stackoverflow Exception in Gson
问题描述
我正在尝试使用Gson库将Json字符串解析为Java对象,但是遇到了StackoverflowException.
I am trying to parse Json string into Java object using Gson library but i encountered StackoverflowException.
java.lang.StackOverflowError
com.google.gson.internal.$Gson$Types.checkNotPrimitive($Gson$Types.java:431)
com.google.gson.internal.$Gson$Types.access$000($Gson$Types.java:42)
com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:540)
com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108)
com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:549)
com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108)
com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:542)
com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108)
com.google.gson.internal.$Gson$Types$WildcardTypeImpl.($Gson$Types.java:549)
com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:108)
Json字符串:
{"password":"ac@123","role":"normaluser","name":"Archana Chatterjee","username":"a.chatterjee","designation":"Teacher","id":"T_02","age":42}
解析代码:
Entity entity = null;
entity = gson.fromJson(json, Staff.class);
Java类:
public class Staff extends LoginEntity {
Logger logger = Logger.getRootLogger();
@SerializedName("name")
String name;
@SerializedName("designation")
String designation;
@SerializedName("role")
String role;
@SerializedName("age")
int age;
}
public abstract class LoginEntity extends Entity {
private static final Logger logger = Logger.getRootLogger();
@SerializedName("username")
String mailid;
@SerializedName("password")
String password;
}
Root class for all.
public abstract class Entity {
Logger logger = Logger.getRootLogger();
@SerializedName("id")
public String id;
}
我还发现了Gson2中的相关错误. .2.2,但我正在使用Gson 2.2.4 .因此,只想确保这是我的错误还是链接中提到的错误.
I also found out related error in Gson2.2.2, but i am using Gson 2.2.4 . So, just want to make sure Is this a error from my side or is it mentioned error in the link.
推荐答案
来自 Gson用户指南:
如果某个字段被标记为临时字段(默认情况下),它将被忽略并且不包含在JSON序列化或反序列化中.
If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.
...
默认情况下,如果将字段标记为瞬态,则将其排除.作为 好吧,如果一个字段被标记为静态",那么默认情况下它将是 排除.
By default, if you mark a field as transient, it will be excluded. As well, if a field is marked as "static" then by default it will be excluded.
因此,解决问题的方法只是将logger
标记为瞬态或静态,例如:
So the solution to your problem is simply to mark your logger
as transient or static, for example:
transient Logger logger = Logger.getRootLogger();
这样,变量将从序列化和反序列化中排除,并且您不会收到该错误.
This way the variable will be excluded from serialization and deserialization, and you won't get that error.
更新:看起来像Gson现在支持 a .excludeFieldsWithoutExposeAnnotation()
并注释要公开的每个字段.
UPDATE: Looks like Gson now supports a @Expose(serialize = boolean)
annotation to explicitly state what you want serialized and what you don't. However, for it to be respected you must call .excludeFieldsWithoutExposeAnnotation()
on your GsonBuilder and annotate every field that you want exposed.
这篇关于Gson中的Stackoverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!