java - Multi level polymorphism in Jackson -
i'd able deserialize json objects having more 1 discriminator column. should allow have set of classes map polymorphic json, lot of possible combinations. wrote hierarchy of classes , used annotations described in documentation. trying deserialize using generic class causes error: seems jackson not walk through entire tree of classes, stops on first level, reading annotations of top class. doing wrong or jackson limitation?
for example, consider following code:
import com.fasterxml.jackson.annotation.jsonsubtypes; import com.fasterxml.jackson.annotation.jsontypeinfo; import com.fasterxml.jackson.databind.jsonmappingexception; import com.fasterxml.jackson.databind.objectmapper; import java.io.ioexception; import static com.fasterxml.jackson.annotation.jsontypeinfo.as.property; import static com.fasterxml.jackson.annotation.jsontypeinfo.id.name; @jsontypeinfo(use = name, include = property, property = "animal") @jsonsubtypes({ @jsonsubtypes.type(value = dog.class, name = "dog"), @jsonsubtypes.type(value = cat.class, name = "cat") }) abstract class animal { } @jsontypeinfo(use = name, include = property, property = "size") @jsonsubtypes({ @jsonsubtypes.type(value = bigdog.class, name = "big"), @jsonsubtypes.type(value = smalldog.class, name = "small") }) abstract class dog extends animal { } class bigdog extends dog { } class smalldog extends dog { } @jsontypeinfo(use = name, include = property, property = "size") @jsonsubtypes({ @jsonsubtypes.type(value = bigcat.class, name = "big"), @jsonsubtypes.type(value = smallcat.class, name = "small") }) abstract class cat extends animal { } class bigcat extends cat { } class smallcat extends cat { } public class polymorphism { public static void main(string args[]) throws ioexception { objectmapper objectmapper = new objectmapper(); try { animal animal1 = objectmapper.readvalue("{\"animal\":\"dog\",\"size\":\"big}", animal.class); system.out.println("this " + animal1.getclass().getsimplename()); } catch (jsonmappingexception e) { system.out.println(e.getmessage()); } try { animal animal2 = objectmapper.readvalue("{\"animal\":\"cat\",\"size\":\"big}", animal.class); system.out.println("this " + animal2.getclass().getsimplename()); } catch (jsonmappingexception e) { system.out.println(e.getmessage()); } try { animal animal3 = objectmapper.readvalue("{\"animal\":\"dog\",\"size\":\"small}", animal.class); system.out.println("this " + animal3.getclass().getsimplename()); } catch (jsonmappingexception e) { system.out.println(e.getmessage()); } try { animal animal4 = objectmapper.readvalue("{\"animal\":\"cat\",\"size\":\"small}", animal.class); system.out.println("this " + animal4.getclass().getsimplename()); } catch (jsonmappingexception e) { system.out.println(e.getmessage()); } } }
i expected obtain following objects: bigdog bigcat smalldog smallcat instead exception thrown.
can clarify if multi level polymorphysm supported jackson? and, if so, correct configuration?
Comments
Post a Comment