json 杰克逊自我参考导致循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10446891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson self reference leading to cycle
提问by elkarel
I have a problem when converting hibernate objects to JSON with Hymanson, because some objects have a self-reference in their definition.
The problem is that I don't control the code of those entities so I cannot put annotations in there.
使用 Hymanson 将休眠对象转换为 JSON 时遇到问题,因为某些对象在其定义中具有自引用。问题是我无法控制这些实体的代码,因此我无法在其中放置注释。
Actually I would like to just limit the depth of the recursivity, for example to 5 levels. I need generic code that works with any entity object that contains self reference.. Is it possible? I don't mind use another JSON library.
实际上,我只想限制递归的深度,例如限制为 5 个级别。我需要适用于任何包含自引用的实体对象的通用代码。这可能吗?我不介意使用另一个 JSON 库。
The following simple code
下面简单的代码
ObjectMapper mapper = new ObjectMapper();
Query q = session.createQuery("from Hazard ");
List<Hazard> hazards = q.list();
for (Hazard h : hazards) {
String hazardJson;
hazardJson = mapper.writeValueAsString(h);
}
Gives me exception:
给我一个例外:
org.codehaus.Hymanson.map.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.fgm.imsma.pojo.Hazard["location"]->com.fgm.imsma.pojo.Location["location"])
at org.codehaus.Hymanson.map.ser.BeanPropertyWriter._reportSelfReference(BeanPropertyWriter.java:473)
at org.codehaus.Hymanson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:411)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)
at org.codehaus.Hymanson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:428)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:245)
at org.codehaus.Hymanson.map.ser.BeanSerializer.serialize(BeanSerializer.java:212)
at org.codehaus.Hymanson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:587)
at org.codehaus.Hymanson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:245)
at org.codehaus.Hymanson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:1993)
at org.codehaus.Hymanson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:1595)
at imsma.json.GetObjects.main(GetObjects.java:47)
Thanks in advance for any suggestions!
在此先感谢您的任何建议!
回答by Eugene Retunsky
Use Hymanson 2.0- it handles cyclic references (with @JsonIdentityInfoannotation)
使用Hymanson 2.0- 它处理循环引用(带@JsonIdentityInfo注释)
If you cannot add annotations directly to the class then use MixIn annotations. An example here: https://github.com/FasterXML/Hymanson-docs/wiki/HymansonMixInAnnotations
如果您不能直接向类添加注释,请使用 MixIn 注释。这里的一个例子:https: //github.com/FasterXML/Hymanson-docs/wiki/HymansonMixInAnnotations

![json 将 NSString 转换为 NSData - [NSString dataUsingEncoding] 异常](/res/img/loading.gif)