简体   繁体   中英

StackOverflowError when trying to serialize from Idml to Json with Jackson

Been using IDMLlib to load IDML files to POJOs, but when I'm trying to serialize the resulting java object to JSON using Jackson JSON , I keep getting into trouble with infinite recursion.

Caused by JsonMappingException: Infinite recursion (StackOverflowError)
(through reference chain: 
de.fhcon.idmllib.api.elements.preferences.Preferences["layoutAdjustmentPreference"]
->de.fhcon.idmllib.api.elements.preferences.LayoutAdjustmentPreference["parent"]
... etc

->>  539 | serializeFields in com.fasterxml.jackson.databind.ser.std.BeanSerializerBase

This happens on multiple occations during parsing.
Idml.document -> Document.root -> Idml.document -> Document.root is a similar one.

I'm using Jackson's ObjectMapper to convert POJO to JSON:

Idml idml = new Idml("test.idml");
ObjectMapper mapper = new ObjectMapper();
String JSONstring = mapper.writeValueAsString(idml);

IDMLlib classes are compiled, so I can't make any changes to them.
Anyone knows a way I can still use the java object IDMLlib returns without having to create my own?

EDIT:
There seems to be a number of references to both "root" and "parent" throughout. So basicly what I'm asking is if anyone knows how to be able to serialize the object without modifying it

Jackson tends to throw this kind of Exception when, for some reasons, it cannot access to one of the properties of the object, or if it cannot serialize one of its properties.

If you don't specify any annotation on the object you are going to serialize, Jackson tries to serialize everything, by going deep into the recursion.

Isn't any chance that you can tell Jackson to ignore some properties or fields?

I had experiences with Pojo being initialized by Hibernate, and I should add "@JacksonIgnore" to every property I had with Hibernate Lazy loading.

If the reference is a simple parent/child type thing (which it sounds like it is), just use annotations:

// on parent object
@JsonManagedReference
public ChildOb child;

// on child object
@JsonBackReference ParentOb parent;

and Jackson will serialize things so that only 'child' is written out (parent not); and when deserializing, it relinks things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM