I’m using jackson 2.0 to serialize a class that has a reference to itself.
Class A{
String identifier;
List<A> related;
}
I want the JSON to look like so:
{ identifier: "name",
related [ identifier: "related to name",
identifier: "also related to name"]
}
Essentially I want to go through one depth of recursion. I’ve tried
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
But it only stops recursion if there is a link back to the original object. How can i fix this?
Jackson 2.0 identity handling is designed to include first instance of an Object as is, and only secondary references using id.
But you could define custom serializer (and deserializer) for your ‘related’ field, to use different serialization style:
and implement
MyIdOnlySerializerso that it only writes outidentifier.