Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Note:</strong> I'm the <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow"><strong>EclipseLink JAXB (MOXy)</strong></a> lead and a member of the <a href="http://jcp.org/en/jsr/detail?id=222" rel="nofollow"><strong>JAXB (JSR-222)</strong></a> expert group.</p> <p>Below is how this can be done using MOXy's JSON-binding.</p> <p><strong>Domain Model (Root)</strong></p> <p>The <code>@XmlElement</code> annotation can be used to specify the type of the property. Setting the type to be <code>Object</code> will force a type qualified to be written out.</p> <pre><code>import javax.xml.bind.annotation.*; public class Root { private String subject; @XmlElement(type=Object.class) public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } } </code></pre> <p><strong>Demo</strong></p> <p>Since a type qualifier will be marshalled out a key will need to be written for the value. By default this will be <code>value</code>. You can use the <code>JSON_VALUE_WRAPPER</code> property to change this to <code>$</code>.</p> <pre><code>import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextProperties; public class Demo { public static void main(String[] args) throws Exception { Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;(3); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); properties.put(JAXBContextProperties.JSON_VALUE_WRAPPER, "$"); JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties); Root root = new Root(); root.setSubject("Cabinet model number?"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } } </code></pre> <p><strong>Output</strong></p> <p>Below is the output from running the demo code.</p> <pre><code>{ "subject" : { "type" : "string", "$" : "Cabinet model number?" } } </code></pre> <p><strong>For More Information</strong></p> <ul> <li><a href="http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html" rel="nofollow">http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload