我在我的应用程序中使用Gson生成调试输出
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
gson.toJson(myObject);
但是Gson在尝试序列化数据结构时会抱怨循环引用错误。使用Jackson库可以解决这个问题吗?
更新 Gson 2.3.1:发布于2014年11月20日
Added support to serialize objects with self-referential fields. The self-referential field is set to null in JSON. Previous version of Gson threw a StackOverflowException on encountering any self-referential fields.
The most visible impact of this is that Gson can now serialize Throwable (Exception and Error)
SerializationFeature.FAIL_ON_EMPTY_BEANS
。您的 serialize() 方法应该有throws JsonProcessingException
或者 try/catch 块。 - kiedysktosmapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
- Chris Thornton