json - jaxb issue with xsd:choice and xsd:sequence -
i have schema has below element
<xs:element name="field"> <xs:complextype> <xs:sequence> <xs:element ref="type" minoccurs="1" maxoccurs="1"/> <xs:choice> <xs:sequence> <xs:choice> <xs:element ref="content"/> <xs:sequence> <xs:element ref="cmd"/> <xs:element ref="legend"/> </xs:sequence> </xs:choice> <xs:element ref="id" minoccurs="0"/> </xs:sequence> <xs:sequence> <xs:element ref="name"/> <xs:choice> <xs:sequence> <xs:element ref="value"/> <xs:element ref="label"/> <xs:element ref="optional"/> </xs:sequence> <xs:sequence> <xs:element ref="optional"/> <xs:choice> <xs:sequence> <xs:element ref="options"/> <xs:element ref="id" minoccurs="0"/> </xs:sequence> <xs:sequence> <xs:element ref="label"/> <xs:element ref="options"/> </xs:sequence> </xs:choice> </xs:sequence> <xs:sequence> <xs:element ref="label"/> <xs:element ref="optional"/> <xs:choice minoccurs="0"> <xs:element ref="boxes"/> <xs:element ref="id"/> <xs:element ref="options"/> </xs:choice> </xs:sequence> </xs:choice> </xs:sequence> <xs:sequence> <xs:element ref="id"/> <xs:element ref="name"/> <xs:element ref="label"/> <xs:element ref="optional"/> </xs:sequence> </xs:choice> </xs:sequence> </xs:complextype> </xs:element>
this generate class structure
@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { "content" }) @xmlrootelement(name = "field") public class field { @xmlelementrefs({ @xmlelementref(name = "legend", type = legend.class), @xmlelementref(name = "id", type = jaxbelement.class), @xmlelementref(name = "name", type = jaxbelement.class), @xmlelementref(name = "cmd", type = jaxbelement.class), @xmlelementref(name = "options", type = options.class), @xmlelementref(name = "content", type = content.class), @xmlelementref(name = "type", type = jaxbelement.class), @xmlelementref(name = "label", type = label.class), @xmlelementref(name = "boxes", type = boxes.class), @xmlelementref(name = "optional", type = jaxbelement.class), @xmlelementref(name = "value", type = jaxbelement.class) }) protected list<object> content; /** * gets rest of content model. * * <p> * getting "catch-all" property because of following reason: * field name "optional" used 2 different parts of schema. see: * line 102 of file:/c:/workspaces/src/main/resources/webforms.xsd * line 99 of file:/c:/workspaces/src/main/resources/webforms.xsd * <p> * rid of property, apply property customization 1 * of both of following declarations change names: * gets value of content property. * * <p> * accessor method returns reference live list, * not snapshot. therefore modification make * returned list present inside jaxb object. * why there not <code>set</code> method content property. * * <p> * example, add new item, follows: * <pre> * getcontent().add(newitem); * </pre> * * * <p> * objects of following type(s) allowed in list * {@link legend } * {@link jaxbelement }{@code <}{@link string }{@code >} * {@link jaxbelement }{@code <}{@link string }{@code >} * {@link jaxbelement }{@code <}{@link string }{@code >} * {@link jaxbelement }{@code <}{@link string }{@code >} * {@link content } * {@link options } * {@link label } * {@link jaxbelement }{@code <}{@link string }{@code >} * {@link jaxbelement }{@code <}{@link boolean }{@code >} * {@link boxes } * * */ public list<object> getcontent() { if (content == null) { content = new arraylist<object>(); } return this.content; } }
in class fields basic fields assigned type = jaxbelement.class (ex.-id declared xsd:string in actual schema) trying generate json object not generating because assigned jaxbelement generated response field
{"declaredtype":"java.lang.string","name":"type","value":"textarea","scope":"javax.xml.bind.jaxbelement$globalscope","nil":false,"globalscope":true,"typesubstituted":false}
i looking json response {type:textarea}
please help
i solved problem writing custom serializer field below
public class customeserializer2 extends jsonserializer {
@override public void serialize(field field, jsongenerator jgen, serializerprovider provider) throws ioexception, jsonprocessingexception { jgen.writestartobject(); jaxbelement jaxbelement; (object object : field.getcontent()) { if (object.getclass().isassignablefrom(jaxbelement.class)) { jaxbelement = (jaxbelement) object; // jgen.writefieldname(jaxbelement.getname().tostring()); jgen.writestringfield(jaxbelement.getname().tostring(), jaxbelement.getvalue().tostring());// terawvalue(jaxbelement.getvalue().tostring()); } else { //jgen.writestartobject(); jgen.writeobjectfield(object.getclass().getsimplename(),object); //jgen.writeendobject(); } } jgen.writeendobject(); } }
and registering object mapper like
simplemodule module = new simplemodule("customeserializermodule", new version(1, 0, 0, null)); module.addserializer(field.class, new customeserializer2()); mapper.registermodule(module);
Comments
Post a Comment