I am using SpringMVC 3.2.4 and want to use Jackson2 to serialize an object in JSON output.
An object has a recursive property. If I try to serialize it using the standard Jackson ObjectMapper, I get a recursion error. I understand that I can use to prevent recursion @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class), but I really want recursion to simplify the analysis in the mustache pattern. However, I would like to limit the level of recursion.
Does Jackson serializer need to be only level 1 at all? If I need to create my own serializer, can I register it so that it is used only for a specific class of objects?
As you can see from a few comments, there is already another SO question that comes very close to this: Jackson JSON serialization, preventing recursion by level detection . However, in this question, the accepted answer (like the others) shows how to avoid Jackson recursion using @JsonIdentityInfo. In this particular case, I am not going to limit it; rather, I want this. However, I just want to limit the depth of the recursion.
In addition, the SO question link provides links to some of Jackson's documents; I already have documents, but Jackson's documents, frankly, are completely absent. They indicate how to register the serializer, but do not indicate how it should be structured. There is also no guidance on how to determine the recursive level of the serializer. Finally, there is no indication if / how you can register a serializer for Jackson in Spring to apply to certain class types.
source
share