1

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

Steffen
  • 325
  • 1
  • 4
  • 9
  • 1
    Retried, and got the document->root problem mentioned earlier. Main problem remains the same, though. Failure: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: de.fhcon.idmllib.api.elements.Document["root"]-> de.fhcon.idmllib.api.elements.Idml["document"]-> de.fhcon.idmllib.api.elements.Document["root"]->...etc – Steffen Oct 02 '12 at 13:07
  • 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. – Steffen Oct 02 '12 at 14:02

2 Answers2

0

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.

emas
  • 668
  • 8
  • 17
  • I can't edit any of the IDMLlib domain classes, as they're a third-party product. That's my problem. – Steffen Oct 02 '12 at 12:54
0

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.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Not necessarily, if you use "mix-in annotations"; so you can either include it there, or use mix-ins. – StaxMan Oct 03 '12 at 20:49
  • Tried, but couldn't quite figure the Mix-ins out. The object IDMLlib returns contains more objects, which contains more objects, etc., all of which are compiled. There's about 3-400 of them in total. Even if i cut down to about 50-100 necessary for my task, would I have to create one Mix-in for each class? – Steffen Oct 04 '12 at 07:39
  • Yes. You can apply them to super-classes (annotations are inherited), itf that helps. Alternatively, if you already have some aspects (annotations, or anything else specific to fields/methods), you could sub-class `AnnotationIntrospector` to make use of non-Jackson annotations. – StaxMan Oct 04 '12 at 21:13
  • Main problem is that I'm fairly new to Jackson, and Json in general. Making a ton of Mix-ins at the moment, but it doesn't seem like the most optimal way by any means. – Steffen Oct 05 '12 at 07:06
  • Thanks for all feedback so far, by the way! – Steffen Oct 05 '12 at 07:07
  • Yup, understood. I am just pointing out things that exist. Btw, if names are indeed constant "root", "parent", `AnnotationIntrospector` could be made to infer managed/parent references, just as if there were annotations there. You'd need maybe 10-20 lines of code for that; basically find places in `JacksonAnnotationIntrospector` implementation where it handles these 2 annotations, replace with your own application-specific logic. HTH. – StaxMan Oct 05 '12 at 20:50
  • I appriciate it, StaxMan. New problems emerged; [posted here](http://stackoverflow.com/questions/12779278/jsonmappingexception-was-java-lang-nullpointerexception). Considering leaving this route all together and just make a new object to parse. Seems easier to handle all this in Java instead of fiddling with the parser. – Steffen Oct 08 '12 at 10:21
  • That may make sense: two-phase handling is a common technique when sequence of two parts is simpler than combination. – StaxMan Oct 08 '12 at 18:51