java - Deserializing cyclic JSON with only one class involved with Jackson -
is possible deserialize following class jackson?
so original version of question wasn't entirely accurate. here's minimal example reproduce problem.
import java.io.ioexception; import com.fasterxml.jackson.annotation.jsoncreator; import com.fasterxml.jackson.annotation.jsonidentityinfo; import com.fasterxml.jackson.annotation.jsonproperty; import com.fasterxml.jackson.annotation.objectidgenerators; import com.fasterxml.jackson.databind.objectmapper; @jsonidentityinfo( generator = objectidgenerators.intsequencegenerator.class, property = "id") public class thing { public thing thing; @jsoncreator public thing(@jsonproperty("thing") thing thing) { this.thing = thing; } public static void main(string[] args) throws ioexception { objectmapper mapper = new objectmapper(); thing cyclic = new thing(null); cyclic.thing = cyclic; string serialised = mapper.writevalueasstring(cyclic); system.out.println(serialised); thing deserialised = mapper.readerfor(thing.class).readvalue(serialised); system.out.println(deserialised.thing == deserialised); } }
this causes unresolved forward reference exception. issue seems jackson told use annotated constructor, can't due cyclic dependency.
the solution add default constructor, , remove @jsonproperty
, @jsoncreator
annotations.
Comments
Post a Comment