Search in sources :

Example 1 with KVPairs

use of com.zimbra.soap.jaxb.KVPairs in project zm-mailbox by Zimbra.

the class JaxbToJsonTest method keyValuePairs.

/**
 * Desired JSON :
 * {
 *   "_attrs": {
 *     "key1": "value1",
 *     "key2": [
 *       "value2-a",
 *       "value2-b"]
 *   },
 *   "_jsns": "urn:zimbraTest"
 * }
 */
@Test
public void keyValuePairs() throws Exception {
    Element jsonElem = JSONElement.mFactory.createElement(QName.get("key-value-pairs", "urn:zimbraTest"));
    jsonElem.addKeyValuePair("key1", "value1");
    jsonElem.addKeyValuePair("key2", "value2-a");
    jsonElem.addKeyValuePair("key2", "value2-b");
    KVPairs kvPairs = new KVPairs();
    kvPairs.addKeyValuePair(new KeyValuePair("key1", "value1"));
    kvPairs.addKeyValuePair(new KeyValuePair("key2", "value2-a"));
    kvPairs.addKeyValuePair(new KeyValuePair("key2", "value2-b"));
    Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(kvPairs);
    KVPairs roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, KVPairs.class);
    logDebug("JSONElement (for comparison) ---> prettyPrint\n%1$s", jsonElem.prettyPrint());
    logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
    List<Element.KeyValuePair> elemKVPs = jsonJaxbElem.listKeyValuePairs();
    Assert.assertEquals("elemKVP num", 3, elemKVPs.size());
    List<KeyValuePair> kvps = roundtripped.getKeyValuePairs();
    Assert.assertEquals("roundtripped kvps num", 3, kvps.size());
    Assert.assertEquals("prettyPrint", jsonElem.prettyPrint(), jsonJaxbElem.prettyPrint());
}
Also used : KeyValuePair(com.zimbra.soap.type.KeyValuePair) 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) KVPairs(com.zimbra.soap.jaxb.KVPairs) 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 KVPairs (com.zimbra.soap.jaxb.KVPairs)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 KeyValuePair (com.zimbra.soap.type.KeyValuePair)1 XmlAnyElement (javax.xml.bind.annotation.XmlAnyElement)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Test (org.junit.Test)1