use of com.zimbra.soap.jaxb.XmlElemJsonAttr in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method exerciseZimbraJsonAttribute.
/**
* <p>Exercise annotation {@link ZimbraJsonAttribute} which is needed in JAXB in addition to {@link XmlElement} or
* {@link XmlElementRef} annotations to provide a field which as an analog of something like:
* <pre>
* jsonElem.addAttribute("xml-elem-json-attr", "XML elem but JSON attribute", Element.Disposition.CONTENT);
* </pre>
* Desired XML serialization:
* <pre>
* <XmlElemJsonAttr xmlns="urn:zimbraTest">
* <xml-elem-json-attr>XML elem but JSON attribute</xml-elem-json-attr>
* <classic-elem>elem for both XML and JSON</classic-elem>
* </XmlElemJsonAttr>
* </pre>
* Desired JSON serialization:
* <pre>
* {
* "xml-elem-json-attr": "XML elem but JSON attribute",
* "classic-elem": [{
* "_content": "elem for both XML and JSON"
* }],
* "_jsns": "urn:zimbraTest"
* }
* </pre>
*/
@Test
public void exerciseZimbraJsonAttribute() throws Exception {
final String str1 = "XML elem but JSON attribute";
final String str2 = "elem for both XML and JSON";
Element jsonElem = JSONElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
Element xmlElem = XMLElement.mFactory.createElement(QName.get("XmlElemJsonAttr", "urn:zimbraTest"));
jsonElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
jsonElem.addElement("classic-elem").setText(str2);
xmlElem.addAttribute("xml-elem-json-attr", str1, Element.Disposition.CONTENT);
xmlElem.addElement("classic-elem").setText(str2);
logDebug("XMLElement ---> prettyPrint\n%1$s", xmlElem.prettyPrint());
logDebug("JSONElement ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
XmlElemJsonAttr jaxb = new XmlElemJsonAttr(str1, str2);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(jaxb);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
Assert.assertEquals("JSONElement and JSONElement from JAXB", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
XmlElemJsonAttr roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, XmlElemJsonAttr.class);
Assert.assertEquals("roundtripped xml-elem-json-attr", str1, roundtripped.getXmlElemJsonAttr());
Assert.assertEquals("roundtripped classic-elem", str2, roundtripped.getDefaultElem());
Assert.assertEquals("JSONElement xml-elem-json-attr as attribute", str1, jsonElem.getAttribute("xml-elem-json-attr"));
// Note difference from XMLElement - for JSON this is Always an attribute but for XML it can be treated as an element
Assert.assertEquals("JSONElement num xml-elem-json-attr elements", 0, jsonElem.listElements("xml-elem-json-attr").size());
}
Aggregations