XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] RE: How to represent mixed content in JSON and JSON Schema?

Shouldn't the "recommended approach" since you used that phrase be not to do it at all.

On Mon, Jul 16, 2018 at 2:56 PM, Costello, Roger L. <costello@mitre.org> wrote:

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": {
               
"@dir": "ltr",
               
"$": "not this text"
          
}

 

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.

 

  • Per Rule #3 attribute/value pairs are represented as JSON object property/value pairs. Per this rule, embed each property/value pair within a JSON object.
  • Per Rule #2 text nodes are represented as JSON object property/value pairs. Per this rule, embed each property/value pair within a JSON object.
  • Per Rule #1 elements are represented as JSON object property/value pairs. Per this rule, embed each property/value pair within a JSON object.

 

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": [
              
{"@dir": "rtl"},
              
{"$": "reverse this text"},
              
{"bdo": {
                                 
"@dir": "ltr",
                                
"$": "not this text"
                           
}
              
},
             
{"$": "finishing text"}
         
]

 

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": [
   
{"$": "some text "},
   
{"bdo": [
       
{"@dir": "rtl"},
       
{"$": "reverse this text"},
       
{"bdo": {
           
"@dir": "ltr",
           
"$": "not this text"
         
}
       
},
       
{"$": "finishing text"}
     
]
   
}
 
]
}

 

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




[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS