The SimpleJson format is the format used by default by the SDK to conver between Campaign XML and JSON
<book title="A confederacy of dunces"> { <author>John Kennedy Toole</author> "$author": "John Kennedy Toole", "chapter": [ <chapter name="Chapter I" pages="20"> { "name": "Chapter I", </chapter> "pages": 20 }, <chapter name="Chapter II" pages="34"> { "name": "Chapter II", </chapter> "pages": 3 }, ] </book> }
The XML root element tag is automatically determined by the SDK as it's generating the XML, usually from the current schema name.
XML: <root/> JSON: {}
XML attributes are mapped to JSON attributes with the same name, whose literal value can be a string, number, or boolean. There's no "@" sign in the JSON attribute name. Values in JSON attributes can be either typed (ex: number, boolean), or strings (ex: "3" instead of just 3) depending if the conversion could determine the attribute type or not. API users should expect and handle both value and use the XtkCaster object to ensure proper conversion.
XML <root hello="world" count="3" ok="true"/> JSON: { hello:"world", count:"3", ok:"true" }
XML elements are mapped to JSON objects
XML: <root><item id="1"/></root> JSON: { item: { id:"1" } }
If the parent element tag ends with -collection children are always an array, even if there are no children, or if there is just one child. The rationale is that XML/JSON conversion is ambigous : XML can have multiple elements with the same tag and when there's only one such element, it's not possible to determine if it should be represented as a JSON object or JSON array unless we have additional metadata.
XML: <root-collection><item id=1/></root> JSON: { item: [ { id:"1" } ] }
When an XML element is repeated, an JSON array is used
XML: <root><item id=1/><item id=2/></root> JSON: { item: [ { id:"1" }, { id:"2" } ] }
Text of XML element is handled with the $ sign in the JSON attribute name, or with a child JSON object name $
Text of the root element
XML: <root>Hello</root> JSON: { $: "Hello" }
Text of a child element
XML: <root><item>Hello</item></root>` JSON: { $item: "Hello" } Alternative JSON: { item: { $: "Hello" } }
If an element contains both text, and children, you need to use the alternative $ syntax
XML: <root><item>Hello<child id="1"/> </item> </root> JSON: { item: { $: "Hello", child: { id:"1" } }
Normally Campaign will not generate XML with elements containing multiple sibling text nodes. If this should happen, the SDK will consider them as a single text value, i.e. it will concatenate the contents of each text and CDATA node as if there was only one. However, whitespaces are processed independently for each text node.
In XML documents, whitespaces can be either significant or insignificant. The SDK uses the following rules to strip whitespaces, which correspond to how Campaign specifically uses XML
Whitespace trimming consists of removing all the leading and trailing whitespaces of each text node and concatenating all text node values. If the resulting value is empty, the text node is ignored. The rationale is to remove insignificant spaces created when formatting XML documents
If an element contains another element and an attribute which have the same name, the attribute name is prefixed with the "@" character
XML: <root timezone="Europe/Paris"> <timezone>Europe/Paris</timezone> </root> JSON: { "@timezone": "Europe/Paris", timezone: { "$": "Europe/Paris" } }
XML: <root> <item>One</item> <item>Two</item> </root> JSON: { item: [ { $: "One" }, { $: "Two" } ] }
A few gaps were identified regarding SimpleJson format when converting from XML to JSON. They were fixed in release 1.1.15 and may introduce some behavior changes for cases which were ambiguous