Hi Folks, I have examined several approaches that people have developed to represent mixed content in JSON. I have identified their best features.
I created an approach that incorporates all their best features.
Below is my recommended approach for representing mixed content in JSON. I welcome your comments and critiques. I will illustrate my approach by showing how to convert the following XML containing mixed content to JSON: <example>Some
text <bdo
dir="rtl">reverse
this text <bdo
dir="ltr">not
this text</bdo></bdo>
finishing text</example> Rule #1: Represent each XML element as a JSON object property, where the property name is the element name. The <example> element is represented as this JSON object property: "example":
… Rule #2: Represent each XML text node as a JSON object property, where the property name is $ and the property value is a string corresponding to the string
value of the text node. The text node within this <bdo> element: <bdo
dir="ltr">not
this text</bdo> is represented by this JSON property/value pair: "$":
"not this text" Rule #3: Represent each XML attribute/value pair as a JSON object property/value pair, where the property name is the attribute name preceded by the @ symbol
and the property value is a string corresponding to the string value of the attribute value. The dir=”ltr” within this <bdo> element: <bdo
dir="ltr">not
this text</bdo> is represented by this JSON property/value pair: "@dir":
"ltr" Rule #4: If an element’s content is not mixed (mixed=”false”), then represent its content as a JSON object: The following <bdo> element does not have mixed content: <bdo
dir="ltr">not
this text</bdo> Applying Rules 1-4 yields this JSON: "bdo":
{ Rule #5: If an element has mixed content, then represent the element’s content (including attribute/value pairs) in JSON as an array, where each item is embedded
in a JSON object. The attributes must be the first items in the array. The remaining JSON array items must be in the same order as they are in the XML mixed content.
The outer <bdo> element has mixed content: <bdo
dir="rtl">reverse
this text <bdo
dir="ltr">not
this text</bdo></bdo> Represent it in JSON this way: "bdo":
[ Rule #6: If an element is the root element, embed it in a JSON object. The <example> element is the root element, so it is embedded in a JSON object: {"example":
[ Read as: This JSON represents an <example> element which has mixed content. The mixed content contains text (some
text), followed by a <bdo> element, followed by text (finishing text). The outer <bdo> element contains mixed content and an attribute dir with value
rtl. Its mixed content is text (reverse this text) followed by an inner <bdo> element. The inner <bdo> element contains text (not this text) and an attribute dir with value
ltr. Comments? /Roger |