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());
}
Aggregations