Jackson to include 'class' field when serializing object to json -
i need grails app communicate (json) client uses jackson. default grails include 'class' in generated json. on other hand, default jackson's objectmapper
doesn't recognize 'class', throws error.
i've found solution, adding @jsontypeinfo(use=jsontypeinfo.id.class, include=jsontypeinfo.as.property, property="class")
on each pojo classes being used.
but, don't prefer approach. because pojo have dependency jackson's jar. there easy alternative?
i trying figure out objectmapper.getserializationconfig(). ...
, still couldn't found alternative replace annotation.
thanks
are using type info polymorphic instantiation or ignoring information? use instead @jsonignoreproperties({"class"})
on classes.
if don't want add annotation classes, can use mix-in mechanism. although more easier add annotation directly classes, if control classes of course.
mapper.addmixinannotations(yourclass.class, typeinfomixin.class); mapper.addmixinannotations(yoursecondclass.class, typeinfomixin.class); // etc. @jsontypeinfo(use=jsontypeinfo.id.class, include=jsontypeinfo.as.property, property="class") public class typeinfomixin {}
Comments
Post a Comment