Search in sources :

Example 1 with XmlElemJsonAttr

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>
     *     &lt;XmlElemJsonAttr xmlns="urn:zimbraTest">
     *       &lt;xml-elem-json-attr>XML elem but JSON attribute&lt;/xml-elem-json-attr>
     *       &lt;classic-elem>elem for both XML and JSON&lt;/classic-elem>
     *     &lt;/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());
}
Also used : XmlAnyElement(javax.xml.bind.annotation.XmlAnyElement) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) JSONElement(com.zimbra.common.soap.Element.JSONElement) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElemJsonAttr(com.zimbra.soap.jaxb.XmlElemJsonAttr) FilterTest(com.zimbra.soap.mail.type.FilterTest) Test(org.junit.Test)

Aggregations

Element (com.zimbra.common.soap.Element)1 JSONElement (com.zimbra.common.soap.Element.JSONElement)1 XMLElement (com.zimbra.common.soap.Element.XMLElement)1 XmlElemJsonAttr (com.zimbra.soap.jaxb.XmlElemJsonAttr)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Test (org.junit.Test)1