java - Avoid creation of object wrapper type/value in MOXy (JAXB+JSON) -
i'm using moxy 2.6 (jaxb+json).
i want objectelement , stringelement marshalled same way, moxy creates wrapper object when fields typed object.
objectelement.java
public class objectelement { public object testvar = "testvalue"; }
stringelement.java
public class stringelement { public string testvar = "testvalue"; }
demo.java
import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import org.eclipse.persistence.jaxb.jaxbcontextfactory; import org.eclipse.persistence.jaxb.marshallerproperties; import org.eclipse.persistence.oxm.mediatype; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontextfactory.createcontext(new class[] { objectelement.class, stringelement.class }, null); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshallerproperties.media_type, mediatype.application_json); system.out.println("objectelement:"); objectelement objectelement = new objectelement(); marshaller.marshal(objectelement, system.out); system.out.println(); system.out.println("stringelement:"); stringelement stringelement = new stringelement(); marshaller.marshal(stringelement, system.out); system.out.println(); } }
when launching demo.java, here's output ...
objectelement: {"testvar":{"type":"string","value":"testvalue"}} stringelement: {"testvar":"testvalue"}
how configure moxy/jaxb make objectelement render stringelement object ? how avoid creation of object wrapper "type" , "value" properties ?
you can use annotation javax.xml.bind.annotation.xmlattribute
. render objectelement , stringelement same output.
see following example:
import javax.xml.bind.annotation.xmlattribute; public class objectelement { @xmlattribute public object testvar = "testvalue"; }
i've used following test class verify correct behaviour.
edit after question updated:
yes it's possible. instead of using xmlattribute before, switched javax.xml.bind.annotation.xmlelement
in combination type attribute.
the class declared as:
public class objectelement { @xmlelement(type = string.class) public object testvar = "testvalue"; }
Comments
Post a Comment