Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping freeform XML/JSON to Moxy/JAXB annotated class
    text
    copied!<p>I'm trying to find a way to correctly <strong>map</strong> the following <strong>XML/JSON document</strong> to an equivalent <strong>JAXB/Moxy annotated class</strong>.<br/> NOTE that the <em>model</em> element of the document, which in my example describes a person, is <strong>freeform</strong>, i.e. might be <strong>any kind of XML element/JSON object</strong>, which is not statically known.</p> <p><strong>XML document:</strong> </p> <pre><code>&lt;form&gt; &lt;title&gt;Person Form&lt;/title&gt; &lt;model&gt; &lt;person&gt; &lt;name&gt;John&lt;/name&gt; &lt;surname&gt;Smith&lt;/surname&gt; &lt;address&gt; &lt;street&gt;Main St.&lt;/street&gt; &lt;city&gt;NY&lt;/city&gt; &lt;country&gt;USA&lt;/country&gt; &lt;/address&gt; &lt;person&gt; &lt;/model&gt; &lt;/form&gt; </code></pre> <p><strong>Equivalent JSON document:</strong></p> <pre><code>{ "title":"Form Title", "model":{ "person":{ "name":"John", "surname":"Smith", "address":{ "street":"Main St.", "city":"NY", "country":"USA" } } } } </code></pre> <p>I thought to map the <em>model</em> field as a <strong>Map</strong>, where the <strong>values</strong> might be <strong>primitive types</strong> or <strong>Map themselves</strong>. This mapping would be enough expressive for my needs.</p> <p>I tried to play with the @XmlReadTransformer, @XmlWriteTransformer MOXY annotations, but with no success (the record parameter I get in the buildAttributeValue is always null)</p> <pre><code>@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Form { private String title; private Model model; ....getters and setters.... } @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Model { @XmlElement @XmlReadTransformer(transformerClass = Transformer.class) @XmlWriteTransformers({ @XmlWriteTransformer(xmlPath = "./*", transformerClass = Transformer.class) }) private Map&lt;String, Object&gt; data; public Map&lt;String, Object&gt; getData() { return data; } public void setData(Map&lt;String, Object&gt; data) { this.data = data; } public static class Transformer implements AttributeTransformer, FieldTransformer { private AbstractTransformationMapping tm; public Transformer() { } @Override public void initialize(AbstractTransformationMapping tm) { this.tm = tm; } @Override public Map&lt;String, Object&gt; buildAttributeValue(Record r, Object o, Session s) { Map&lt;String, Object&gt; data = new HashMap&lt;String, Object&gt;(); // TODO: ???? return data; } @Override public Object buildFieldValue(Object arg0, String arg1, Session arg2) { // TODO return null; } } } </code></pre> <p>Can you suggest me a proper way of solve this problem or a different way of modeling the "model" field?</p>
 

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