Loading ...
Sorry, an error occurred while loading the content.

JSONML

Expand Messages
  • Douglas Crockford
    There are two classes of transformations between JSON and XML: The Data Transform and the Document Transform. The Data transform loses some of the structure
    Message 1 of 5 , Apr 25, 2008
    • 0 Attachment
      There are two classes of transformations between
      JSON and XML: The Data Transform and the Document
      Transform.

      The Data transform loses some of the structure
      components of an XML text in order to produce a JSON
      representation that is lighter and easier to work
      with. For example, the XML text

      <a ichi="1" ni="2"><b>The content of b</b> and
      <c san="3">The content of c</c><d>do</d><e></e>
      <d>re</d><f/><d>mi</d></a>

      could be transformed into this JSON text:

      {"a": {
      "b": "The content of b",
      "c": {
      "content": "The content of c",
      "san": "3"
      },
      "content": "and",
      "d": [
      "do",
      "re",
      "mi"
      ],
      "e": "",
      "f": {},
      "ichi": "1",
      "ni": "2"
      }}

      The Data transform maps tag names to member names.
      Arrays are introduced where names are not unique, as
      can be seen in the combining of the <d> tags. Ordering
      information is lost, so the JSON structure does not
      capture the interleaving of the <e> and <f> in the <d>s.

      The Document transform preserves the structural
      information, allowing for nearly lossless
      transformations. The Document transform of the same
      XML text yields

      [
      "a",
      {
      "ichi": "1",
      "ni": "2"
      },
      [
      "b",
      "The content of b"
      ],
      "and",
      [
      "c",
      {"san": "3"},
      "The content of c"
      ],
      [
      "d",
      "do"
      ],
      ["e"],
      [
      "d",
      "re"
      ],
      ["f"],
      [
      "d",
      "mi"
      ]
      ]

      Each tag is represented by an array. The first element of
      an array is the tag name. The second element may be an
      object containing the attribues, and the remaining elements
      contain the content and child elements. The only information
      lost in this example is the shape of <f></f> compared to the
      more compact <e/>.

      The Document transform is sometimes called JsonML. A
      description of JsonML can be found at
      http://www.ibm.com/developerworks/library/x-jsonml/

      I have added a JSONML class to the Java reference
      implementation package org.json. It provides toJSONArray and
      toString methods that convert XML text to JSON structures and
      back.
    • Atif Aziz
      ... Shouldn t the value of content read, and ? That is, the space surrounding add would be preserved, right? ... Did you mean the other way around? ...
      Message 2 of 5 , Apr 28, 2008
      • 0 Attachment
        > "content": "and",

        Shouldn't the value of "content" read, " and "? That is, the space surrounding "add" would be preserved, right?

        > is the shape of <f></f> compared to the
        > more compact <e/>.

        Did you mean the other way around?

        > The only information
        > lost in this example is the shape of <f></f> compared to the
        > more compact <e/>.

        Couldn't this even be preserved by using an empty string as the second or third element (depending on presence of attributes)? That is, if ["e"] were emitted as ["e",""] then the shape could be maintained.

        - Atif


        From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of Douglas Crockford
        Sent: Saturday, April 26, 2008 2:46 AM
        To: json@yahoogroups.com
        Subject: [json] JSONML

        There are two classes of transformations between
        JSON and XML: The Data Transform and the Document
        Transform.

        The Data transform loses some of the structure
        components of an XML text in order to produce a JSON
        representation that is lighter and easier to work
        with. For example, the XML text

        <a ichi="1" ni="2"><b>The content of b</b> and
        <c san="3">The content of c</c><d>do</d><e></e>
        <d>re</d><f/><d>mi</d></a>

        could be transformed into this JSON text:

        {"a": {
        "b": "The content of b",
        "c": {
        "content": "The content of c",
        "san": "3"
        },
        "content": "and",
        "d": [
        "do",
        "re",
        "mi"
        ],
        "e": "",
        "f": {},
        "ichi": "1",
        "ni": "2"
        }}

        The Data transform maps tag names to member names.
        Arrays are introduced where names are not unique, as
        can be seen in the combining of the <d> tags. Ordering
        information is lost, so the JSON structure does not
        capture the interleaving of the <e> and <f> in the <d>s.

        The Document transform preserves the structural
        information, allowing for nearly lossless
        transformations. The Document transform of the same
        XML text yields

        [
        "a",
        {
        "ichi": "1",
        "ni": "2"
        },
        [
        "b",
        "The content of b"
        ],
        "and",
        [
        "c",
        {"san": "3"},
        "The content of c"
        ],
        [
        "d",
        "do"
        ],
        ["e"],
        [
        "d",
        "re"
        ],
        ["f"],
        [
        "d",
        "mi"
        ]
        ]

        Each tag is represented by an array. The first element of
        an array is the tag name. The second element may be an
        object containing the attribues, and the remaining elements
        contain the content and child elements. The only information
        lost in this example is the shape of <f></f> compared to the
        more compact <e/>.

        The Document transform is sometimes called JsonML. A
        description of JsonML can be found at
        http://www.ibm.com/developerworks/library/x-jsonml/

        I have added a JSONML class to the Java reference
        implementation package org.json. It provides toJSONArray and
        toString methods that convert XML text to JSON structures and
        back.
      • John Cowan
        ... This is a distinction without a difference. No XML software (other than perhaps some XML editors) maintains a difference between and ; they
        Message 3 of 5 , Apr 28, 2008
        • 0 Attachment
          Atif Aziz scripsit:

          > > The only information
          > > lost in this example is the shape of <f></f> compared to the
          > > more compact <e/>.
          >
          > Couldn't this even be preserved by using an empty string as the second
          > or third element (depending on presence of attributes)? That is, if
          > ["e"] were emitted as ["e",""] then the shape could be maintained.

          This is a distinction without a difference. No XML software (other
          than perhaps some XML editors) maintains a difference between <a></a>
          and <a/>; they are exact synonyms.

          --
          John Cowan cowan@... http://ccil.org/~cowan
          "The exception proves the rule." Dimbulbs think: "Your counterexample proves
          my theory." Latin students think "'Probat' means 'tests': the exception puts
          the rule to the proof." But legal historians know it means "Evidence for an
          exception is evidence of the existence of a rule in cases not excepted from."
        • Michael Schøler
          ... What about and then (the tag itself not being important)? I would argue that the two are not synonyms as
          Message 4 of 5 , Apr 28, 2008
          • 0 Attachment
            --- In json@yahoogroups.com, John Cowan <cowan@...> wrote:
            >
            > This is a distinction without a difference. No XML software (other
            > than perhaps some XML editors) maintains a difference between <a></a>
            > and <a/>; they are exact synonyms.

            What about <sometag attr=""></sometag> and <sometag></sometag> then
            (the tag itself not being important)?

            I would argue that the two are not synonyms as emptying an attribute
            value could mean overriding a default attribute value.

            I have not investigated myself if this is not supported - I'm only
            curious.

            Best regards
            Michael Schøler
          • John Cowan
            ... You are right: they are not synonyms. -- John Cowan http://www.ccil.org/~cowan Any legal document draws most of its meaning from
            Message 5 of 5 , Apr 28, 2008
            • 0 Attachment
              Michael Schøler scripsit:

              > What about <sometag attr=""></sometag> and <sometag></sometag> then
              > (the tag itself not being important)?
              >
              > I would argue that the two are not synonyms as emptying an attribute
              > value could mean overriding a default attribute value.

              You are right: they are not synonyms.

              --
              John Cowan http://www.ccil.org/~cowan <cowan@...>
              "Any legal document draws most of its meaning from context. A telegram
              that says 'SELL HUNDRED THOUSAND SHARES IBM SHORT' (only 190 bits in
              5-bit Baudot code plus appropriate headers) is as good a legal document
              as any, even sans digital signature." --me
            Your message has been successfully submitted and would be delivered to recipients shortly.