java - How to serialize List<Object> using Jackson without annotations? -
i have class containing list<object>
, field annotated using jaxb annotations indicate possible types inside list. using jaxbannotationmodule
when configuring objectmapper
. json class looking this:
"myclass": { "listfield" : [ "myclasssecond" : {el1}, "myclassthrid" : {el2} ] }
and want change this:
"myclass": [ "myclasssecond" : {el1}, "myclassthrid" : {el2} ]
i can't annotate myclass
because generated. know list contains elements of type myclasssecond
, myclassthird
. classes generated.
currently solution looks this:
public class myclassserializer extends jsonserializer<myclass> { @override public void serialize(myclass value, jsongenerator gen, serializerprovider serializers) throws ioexception, jsonprocessingexception { gen.writestartarray(); (object e : value.getlistfield()) { gen.writestartobject(); gen.writeobjectfield(e.getclass().getsimplename(), e); gen.writeendobject(); } gen.writeendarray(); }
is there possibility rid of e.getclass().getsimplename()
part more elegant solution?
thanks in advance.
Comments
Post a Comment