java - Order matters with class metadata in Genson - Is there a work-around? -
i'm using genson serialize + deserialize json in android app polymorphic objects. json coming variety of sources though , can't guarantee @class metadata first line item in json. walking through genson code , writing test cases looks @class metadata has first entry in dictionary.
has had luck working around constraint? time switch else, , if so, what?
public class message { payload payload; // getters & setters } public abstract class payload { // } public class notification1 extends payload { string text; // getters & setters } public class notification2 extends payload { string othertext // getters & setters } string correctorder = {"@class":"message","payload":{"@class":"notification1","text":"text"}} string modifiedorder = {"@class":"message","payload":{"text":"text", "@class":"notification1"}} genson g = genson.builder() .addalias("notification1", notification1.class) .addalias("notification2", notification2.class) .useruntimetype(true) .useclassmetadata(true) .usemetadata(true) .usefields(false) .useindentation(false) .create(); g.deserialize(correctorder, message.class) // works g.deserialize(modifiedorder, message.class) // barfs error: com.owlike.genson.jsonbindingexception: not deserialize type class com.ol.communication.messages.message
indeed order matters. choosed on purpose, see remarks in the user guide.
if allow @class property anywhere in json object, have first deserialize json object (and sub properties obj/arr etc) intermediary info construction , right type. incur additional memory overhead , less speed greater flexibility, true.
a solution mark classes polymorphic (annotation/config in builder), whom genson search/produce @class property in stream. allow have overhead polymorphic objects in stream.
at moment not implemented, opened an issue. come in future release.
outside of technical aspects, don't think should have polymorphic logic (or other fancy stuff) when dealing multiple external api. mean kind of features library specific, if don't utilize same tool on both sides can run troubles. people have layer used communicate apis , map info model. if don't own code on both ends, think solution on long term.
java android json genson
No comments:
Post a Comment