<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE xep SYSTEM 'xep.dtd' [
  <!ENTITY % ents SYSTEM 'xep.ent'>
%ents;
]>
<?xml-stylesheet type='text/xsl' href='xep.xsl'?>
<xep>
<header>
  <title>JSON Content Type support</title>
  <abstract>This specification defines JavaScript Object Notation (JSON) use in XMPP service.</abstract>
  <legal>
    <copyright>This XMPP Extension Protocol is copyright (c) 1999 - 2009 by the XMPP Standards Foundation (XSF).</copyright>
    <permissions>Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the &quot;Specification&quot;), to make use of the Specification without restriction, including without limitation the rights to implement the Specification in a software program, deploy the Specification in a network service, and copy, modify, merge, publish, translate, distribute, sublicense, or sell copies of the Specification, and to permit persons to whom the Specification is furnished to do so, subject to the condition that the foregoing copyright notice and this permission notice shall be included in all copies or substantial portions of the Specification. Unless separate permission is granted, modified works that are redistributed shall not contain misleading information regarding the authors, title, number, or publisher of the Specification, and shall not claim endorsement of the modified works by the authors, any organization or project to which the authors belong, or the XMPP Standards Foundation.</permissions>
    <warranty>## NOTE WELL: This Specification is provided on an &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. In no event shall the XMPP Standards Foundation or the authors of this Specification be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the Specification or the implementation, deployment, or other use of the Specification. ##</warranty>
    <liability>In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall the XMPP Standards Foundation or any author of this Specification be liable for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising out of the use or inability to use the Specification (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if the XMPP Standards Foundation or such author has been advised of the possibility of such damages.</liability>
    <conformance>This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which may be found at &lt;<link url='http://www.xmpp.org/extensions/ipr-policy.shtml'>http://www.xmpp.org/extensions/ipr-policy.shtml</link>&gt; or obtained by writing to XSF, P.O. Box 1641, Denver, CO 80201 USA).</conformance>
  </legal>
  <number>xxxx</number>
  <status>ProtoXEP</status>
  <type>Standards Track</type>
  <sig>Standards</sig>
  <approver>Council</approver>
  <dependencies>
    <spec>XMPP Core</spec>
    <spec>XEP-xxxx</spec>
    <spec>Etc.</spec>
  </dependencies>
  <supersedes/>
  <supersededby/>
  <shortname>NOT_YET_ASSIGNED</shortname>
  <author>
    <firstname>Mika</firstname>
    <surname>Helander</surname>
    <org>Nokia Corporation</org>
    <email>mika.helander@nokia.com</email>
    <jid>mikah@ovi.com</jid>
  </author>
  <revision>
    <version>0.0.1</version>
    <date>2010-05-07</date>
    <initials>MiH</initials>
    <remark><p>First draft.</p></remark>
  </revision>
</header>
<section1 topic='Introduction' anchor='intro'>
  <p>This spesification defines how JSON data type is used to exchange information between client and XMPP-service. Intended use is in JavaScript-based clients running in web browser, widget runtime etc.</p>
  <p>This JSON handling in service is targetet both for <link url='http://tools.ietf.org/html/rfc3920'>C2S</link> and <link url='http://xmpp.org/extensions/xep-0124.html'>BOSH</link> connections.</p>
</section1>
<section1 topic='Requirements' anchor='reqs'>
  <p>The following design requirements reflect the need to offer performance as close as possible to standard XMPP-based stanza handling.</p>
   <ol>
    <li>JSON default character set must be UTF-8</li>
    <li>JSON stanza must contain (or retain) all XMPP stanza content and hierarchy</li>
    <li>Server must support both XML and JSON content-types.</li>
   </ol>
</section1>
<section1 topic='Use Cases' anchor='usecases'>
  <p>Intent for following use-cases is to support JavaScript-based clients which typically start XMPP-session from HTTP-dialog, and then depending on network environment and run-time support end using BOSH or C2S through Web Sockets.</p>
  <section2 topic='BOSH' anchor='uc-bosh'>
    <p>Initial BOSH request from browser or equivalent uses in http header content-type "application/jsonrequest". This initial request payload is in JSON format, and server responds with same content-type and JSON playload.</p>
    <p>If server doesn't support JSON it responds with http status 400 "Bad Request". In that case client either switches to content-type "text/xml; charset=utf-8" and XML-payload.</p>
  </section2>
  <section2 topic='C2S' anchor='uc-c2s'>
    <p>In Client to Service connection initialization client proposes stream feature "json" TBD...</p>
  </section2>
</section1>
<section1 topic='Implementation Notes' anchor='impl'>
  <p>Client (and server) implementation needs to take care of using such JSON object format which retains all structure of all XMPP XML stanzas.</p>
  <section2 topic='HTTP Header' anchor='http-header'>
   <p>Following http-header is used to communicate with server using JSON playload:</p>
    <section3 topic='Request' anchor='c-header'><code>
POST /http-bind HTTP/1.1
Host: httpcm.jabber.org
Accept-Encoding: gzip, deflate
Content-Type: application/jsonrequest
Content-Length: 230
    </code></section3>
    <section3 topic='Response' anchor='s-header'><code>
HTTP/1.1 200 OK
Content-Type: application/jsonrequest
Content-Length: 513
</code></section3>
  </section2>
  <section2 topic='Initial BOSH request/response in JSON' anchor='init-payload'>
   <p>In following example server name is modified so content length is not accurate. Also JSON payload is modified for better clarity of its structure.</p>
    <section3 topic='Client request' anchor='c-req'><code>
POST /http-bind HTTP/1.1
Host: httpcm.jabber.org
Accept-Encoding: gzip, deflate
Content-Type: application/jsonrequest
Content-Length: 230

{ "body" : {
  "hold" : 1,
  "ver" : "1.6",
  "wait" : 285,
  "requests" : 2,
  "rid" : 2669000493,
  "to" : "server.com",
  "lang" : { "$$" : "xml", "$" : "en" },
  "version" : { "$$" : "xmpp", " : "1.0" },
  "xmlns" : {
    "$", "http://jabber.org/protocol/httpbind",
    "@xmpp" : "urn:xmpp:xbosh" }
  }
}
</code></section3>
    <section3 topic='Server response' anchor='s-resp'><code>
HTTP/1.1 200 OK
Content-Type: application/jsonrequest
Content-Length: 513

{ "body" : {
  "sid" : "b1165122e8856a087c0be4c53c229713a95d8ce6",
  "wait" : "285",
  "requests" : "2",
  "inactivity" : "30",
  "maxpause" : "120",
  "polling" : "2",
  "ver" : "1.6",
  "from" : "server.com",
  "secure" : "true",
  "authid" : "1402940319",
  "xmlns" : {
    "$" : "http://jabber.org/protocol/httpbind",
    "@xmpp" : "urn:xmpp:xbosh",
    "@stream" : "http://etherx.jabber.org/streams"
  },
  "version" : { "$$" : "xmpp", "$" : "1.0" },
  "$" : {
    "features" : {
      "$$" : "stream",
      "$" : {
        "mechanisms" : {
          "xmlns" : "urn:ietf:params:xml:ns:xmpp-sasl",
          "$" : { "mechanism" : "PLAIN" }
        }
      }
    }
  }
}
</code></section3>
    <section3 topic='JSON syntax explained' anchor='json-syntax'>
     <ul>
      <li>Tag with text value
       <em>XMPP-XML:</em><code>
&lt;tag&gt;txt-value&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : "txt-value" }
</code></li>
      <li>Tag with another tag
       <em>XMPP-XML:</em><code>
&lt;tag&gt;
 &lt;tag2&gt;txt-value&lt;/tag2&gt;
&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "$" : {
    "tag2" : "txt-value" }
  }
}
</code></li>
      <li>Multiple text value tags as array
       <em>XMPP-XML:</em><code>
&lt;tag&gt;
 &lt;tag2&gt;txt-value1&lt;/tag2&gt;
 &lt;tag2&gt;txt-value2&lt;/tag2&gt;
&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "$" : {
    "tag2" : [ "txt-value1", "txt-value2" ] }
  }
}
</code></li>
      <li>Tag with attribute, no value
       <em>XMPP-XML:</em><code>
&lt;tag attr="attr-value" /&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : { "attr" : "attr-value" } }
</code></li>
      <li>Tag with multiple attributes as array, no value
       <em>XMPP-XML:</em><code>
&lt;tag attr="attr-value1" attr="attr-value2" /&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "attr" : [ "attr-value1", "attr-value2" ] }
}
</code></li>
      <li>Tags as array with unique attributes, no value
       <em>XMPP-XML:</em><code>
&lt;tag&gt;
 &lt;tag2 attr="attr-value1" /&gt;
 &lt;tag2 attr="attr-value2" /&gt;
&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "tag2" : [
    { "attr" : "attr-value1" },
    { "attr" : "attr-value2" } ]
  }
}
</code></li>
      <li>Tag with namespace attribute, no value
       <em>XMPP-XML:</em><code>
&lt;tag xmlns:ns="ns-value" /&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "xmlns" : {
    "@ns" : "attr-value" }
  }
}
</code></li>
      <li>Tag with many attributes to namespace, no value
       <em>XMPP-XML:</em><code>
&lt;tag xmlns="root-value" xmlns:ns="ns-value" /&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "xmlns" : {
    "$" : "root-value",
    "@ns" : "attr-value" }
  }
}
</code></li>
      <li>Tag with namespace attribute, no value
       <em>XMPP-XML:</em><code>
&lt;ns:tag attr="attr-value" /&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "$$" : "ns",
  "attr" : "attr-value" }
}
</code></li>
      <li>Tag with attribute and text value
       <em>XMPP-XML:</em><code>
&lt;tag attr="attr-value"&gt;txt-value&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "attr" : "attr-value",
  "$" : "txt-value" }
}
</code></li>
      <li>Namespace tag with attribute and text value
       <em>XMPP-XML:</em><code>
&lt;ns:tag attr="attr-value"&gt;txt-value&lt;/tag&gt;
</code>
       <em>JSON:</em><code>
{ "tag" : {
  "$$" : "ns",
  "attr" : "attr-value",
  "$" : "txt-value" }
}
</code></li>
      <li>Complex combination</li>
      <li>Tag name conversions
       <p>JSON data is typically converted to JS-object in browser client. Practically this means that <em>tag string name</em> / <em>value string</em> pairs are converted to <em>tag name</em> / <em>value string</em> pairs. Example:</p><code>
var s = '{ "key" : "value" }';
var sObj = JSON.parse(s);        // sObj = { key : "value" };
var sStr = JSON.stringify(sObj); // sStr = '{"key":"value"}';
</code>
       <p>Javascript variable naming doesn't support full colon characters '<em>:</em>'. Intented conversion between JSON and JS-objects is based on native JavaScript class <em>JSON</em>, more spesifically methods <em>JSON.stringify()</em> for converting object to JSON, and <em>JSON.parse()</em> from JSON to object.<br/>
       Because of this namespace definitions are constructed hiearchically and their scope is within tag it is defined. Currently only reserved namespace name is '<em>xml</em>'.</p></li>
     </ul>
    </section3>
  </section2>
</section1>
<section1 topic='Security Considerations' anchor='security'>
  <p>Same security issues apply as XML-payload. When using JSON class of later html5-supported browsers no need to use eval to convert JSON-string into variable value.</p>
</section1>
<section1 topic='IANA Considerations' anchor='iana'>
  <p>REQUIRED.</p>
</section1>
<section1 topic='XMPP Registrar Considerations' anchor='registrar'>
  <p>The <link url='http://xmpp.org/registrar/'>XMPP Registrar</link> [??] includes 'xx:xx:xx' in its registry of protocol namespaces.</p>
</section1>
<section1 topic='XML Schema' anchor='schema'>
  <p>Makes me wonder how to use XML Schema for playing with JSON payload ?</p>
</section1>
</xep>
