java - Jackson Deserialize to POJO Based On Object Properties -
is possible deserialize json using jackson 1 of 2 types based on content of json?
for example, have following java (technically groovy, that's not important) interfaces , classes:
interface id { thing tothing() } class naturalid implements id { final string packageid final string thingid thing tothing() { new packageidentifiedthing(packageid, thingid) } } class alternateid implements id { final string key thing tothing() { new alternatelyidentifiedthing(key) } }
the json receive either of following:
this json should map naturalid {"packageid": "somepackage", "thingid": "someentity"}
this json should map alternateid {"key": "someuniquekey"}
does know how can accomplish jackson 2.x without including type id's?
are these 2 classes implement id
? if so, write iddeserializer
class , put @jsondeserialize(using = iddeserializer.class)
on id
interface, , deserializer @ json , determine object deserialize into.
edit: jsonparser streaming should this:
public id deserialize(jsonparser jp, deserializationcontext ctxt) throws ioexception { objectnode node = jp.readvalueastree(); class<? extends id> concretetype = determineconcretetype(node); //implement return jp.getcodec().treetovalue(node, concretetype); }
Comments
Post a Comment