xeps/xep-0295.xml

199 行
10 KiB
XML

<?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 Encodings for XMPP</title>
<abstract>This specification defines an alternative JSON encoding for XMPP stanzas and other elements.</abstract>
&LEGALNOTICE;
<number>0295</number>
<status>Active</status>
<type>Humorous</type>
<sig>Standards</sig>
<approver>Council</approver>
<dependencies>
<spec>XMPP Core</spec>
</dependencies>
<supersedes/>
<supersededby><spec>XEP-0335</spec></supersededby>
<shortname>N/A</shortname>
&ksmith;
&mwild;
<revision>
<version>1.0</version>
<date>2011-04-01</date>
<initials>ks, mw</initials>
<remark><p>April Fools!</p></remark>
</revision>
</header>
<section1 topic='Introduction' anchor='intro'>
<p>It has long been known that XML is an outdated, failed format for interchangeable data serialization. While it does, admittedly, provide all the features that XMPP needs, XML is not without its share of detractors. Indeed, some years ago this led to the (sadly short-lived) attempt to provide a binary encoding for XMPP given in &xep0239;. Unfortunately, the binary encoding lacked the main advantages of XML in its human readability, so the search for better encodings has led us to JSON. JSON is a very popular format and it is sensible to utilize this popularity by reframing XMPP stanzas in JSON. JSON is not as expressive as XML in terms of namespacing, so this document presents a method of encoding stanzas, including namespaces in JSON.</p>
<p></p>
</section1>
<section1 topic='Protocol' anchor='protocol'>
<p>The recently updated &xmppcore; documents the legacy XML encoding of XMPP, and readers are urged to refer to that spec not just for other details of the protocol but also to appreciate the relative elegance of the encoding contained within this extension.</p>
<p>Let us consider a fairly standard XMPP message stanza:</p>
<example caption='XML-encoded XMPP stanza'><![CDATA[
<message to="alice@wonderland.lit" id="if00lu" >
<body>Hi you</body>
<body xml:lang="cy">Prynhawn da</body>
<nick xmlns="http://jabber.org/protocol/nick">Alice</nick>
<active xmlns="http://jabber.org/protocol/chatstates"/>
</message>
]]></example>
<p>Given the need to include the namespaces within the JSON, an immediately obvious structure may be something like::</p>
<example caption='Na&#239;ve JSON representation'><![CDATA[
{"stanza-name": "message",
"stanza-namespace": "jabber:client",
"stanza-attributes": {
"to":"alice@wonderland.lit",
"id":"if00lu"
}
"stanza-children": [
{
"element-name": "body",
"element-namespace": "jabber:client",
"element-value": "Hi you"
},
{
"element-name": "body",
"element-namespace": "jabber:client",
"element-language": "cy",
"element-value": "Prynhawn da"
},
{
"element-name": "nick",
"element-namespace": "http://jabber.org/protocol/nick",
"element-value": "Alice"
},
{
"element-name": "active",
"element-namespace": "http://jabber.org/protocol/chatstates",
}
]
}
]]></example>
<p>While many of the advantages of JSON over XML can be observed in this encoding (particularly the inherent brevity), an even more compact representation has been developed. Instead of reserializing the traditional XML stanzas in this manner, it is possible to wrap the stanzas within JSON, thereby enjoying the best of both worlds:</p>
<example caption='Advanced JSON-encoding'><![CDATA[
{"s":"<message to='alice@wonderland.lit' id='if00lu'><body>Hi you</body><body xml:lang='cy'>Prynhawn da</body><nick xmlns='http://jabber.org/protocol/nick'>Alice</nick><active xmlns='http://jabber.org/protocol/chatstates'/></message>"}
]]></example>
<p>To use this improved encoding (eminently suitable both for c2s and s2s connections), entities should follow the connection rules defined in &xmppcore; and immediately start sending JSON-encoded data. Receiving entities should detect the presence of an open-brace ('{') character as the first octet received on a stream to be a signal to continue with JSON encoding. Servers supporting only the legacy XML encoding will necessarily respond with an error when receiving the improved JSON format, and entities will know to reconnect and continue with the legacy format.</p>
</section1>
<section1 topic='Examples' anchor='examples'>
<p>Hopefully the beauty of this approach will be apparent at this stage, but in case some lingering doubts remain (and with the hope of aiding interoperability), more examples are provided here:</p>
<example caption='XML-encoded Message with Security Label'><![CDATA[
<message to='romeo@example.net' from='juliet@example.com/balcony'>
<body>This content is classified.</body>
<securitylabel xmlns='urn:xmpp:sec-label:0'>
<displaymarking fgcolor='black' bgcolor='red'>SECRET</displaymarking>
<label><esssecuritylabel xmlns='urn:xmpp:sec-label:ess:0'>
MQYCAQQGASk=
</esssecuritylabel></label>
</securitylabel>
</message>
]]></example>
<example caption='JSON-encoded Message with Security Label'><![CDATA[
{"s":"<message to='romeo@example.net' from='juliet@example.com/balcony'><body>This content is classified.</body><securitylabel xmlns='urn:xmpp:sec-label:0'><displaymarking fgcolor='black' bgcolor='red'>SECRET</displaymarking><label><esssecuritylabel xmlns='urn:xmpp:sec-label:ess:0'>MQYCAQQGASk=</esssecuritylabel></label></securitylabel></message>"}
]]></example>
<example caption='XML-encoded XHTML-IM message'><![CDATA[
<message from='ladymacbeth@shakespeare.lit/castle'
to='macbeth@chat.shakespeare.lit'
type='groupchat'>
<body>Yet here's a spot.</body>
<html xmlns='http://jabber.org/protocol/xhtml-im'>
<body xmlns='http://www.w3.org/1999/xhtml'>
<p>
Yet here's a spot.
<img alt='A spot'
src='cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org'/>
</p>
</body>
</html>
</message>
]]></example>
<example caption='JSON-encoded XHTML-IM message'><![CDATA[
{"s":"<message from='ladymacbeth@shakespeare.lit/castle' to='macbeth@chat.shakespeare.lit' type='groupchat'><body>Yet here's a spot.</body><html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'><p>Yet here's a spot.<img alt='A spot' src='cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org'/></p></body></html></message>"}
]]></example>
<example caption='XML-encoded Dialback Key Transmission'><![CDATA[
<db:result
from='sender.tld'
to='target.tld'>
1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9
</db:result>
]]></example>
<example caption='JSON-encoded Dialback Key Transmission'><![CDATA[
{"s":"<db:result from='sender.tld' to='target.tld'>1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9</db:result>"}
]]></example>
<example caption='XML-encoded Event Publication'><![CDATA[
<iq from='juliet@capulet.lit/balcony' type='set' id='pub1'>
<pubsub xmlns='http://jabber.org/protocol/pubsub'>
<publish node='http://jabber.org/protocol/tune'>
<item>
<tune xmlns='http://jabber.org/protocol/tune'>
<artist>Gerald Finzi</artist>
<length>255</length>
<source>Music for 'Love's Labors Lost' (Suite for small orchestra)</source>
<title>Introduction (Allegro vigoroso)</title>
<track>1</track>
</tune>
</item>
</publish>
</pubsub>
</iq>
]]></example>
<example caption='JSON-encoded Event Publication'><![CDATA[
{"s":"<iq from='juliet@capulet.lit/balcony' type='set' id='pub1'><pubsub xmlns='http://jabber.org/protocol/pubsub'><publish node='http://jabber.org/protocol/tune'><item><tune xmlns='http://jabber.org/protocol/tune'><artist>Gerald Finzi</artist><length>255</length><source>Music for 'Love's Labors Lost' (Suite for small orchestra)</source><title>Introduction (Allegro vigoroso)</title><track>1</track></tune></item></publish></pubsub></iq>"}
]]></example>
<example caption='XML-encoded Stream Header'><![CDATA[
<stream:stream
from='juliet@example.com'
to='example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
]]></example>
<example caption='JSON-encoded Stream Header'><![CDATA[
{"s":"<stream:stream from='juliet@example.com' to='example.com' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"}
]]></example>
<p>Beautiful, elegant <em>and</em> efficient at the same time.</p>
</section1>
<section1 topic='Internationalization Considerations' anchor='i18n'>
<p>It is hoped that this representation introduces no new internationalization considerations, although it is acknowledged that if there are cultures where the symbols <tt>{}:</tt> are considered to be more offensive than <tt>&lt;>=</tt> the legacy XML encoding may be preferred.</p>
</section1>
<section1 topic='Security Considerations' anchor='security'>
<p>Implementors should be aware that the JSON encoding involves 8 additional bytes for each stanza, and this introduces a considerable risk of buffer over-flow attacks. While new codebases will hopefully be designed with this in mind, existing codebases will need to be entirely upgraded, with every buffer increased in size by at least 8 bytes to address this potentially serious potential vulnerability.</p>
</section1>
<section1 topic='IANA Considerations' anchor='iana'>
<p>None.</p>
</section1>
<section1 topic='XMPP Registrar Considerations' anchor='registrar'>
<p>The XMPP Registrar may wish to consider maintenance of dual registries - for both XML and JSON encodings, but this is OPTIONAL.</p>
</section1>
<section1 topic='JSON Schema' anchor='schema'>
<code><![CDATA[
{
"type": "object",
"additionalProperties": false,
"properties": {
"s": {
"type": "string",
"required": true,
"format": "xml"
}
}
}
]]></code>
</section1>
<section1 topic='Acknowledgements' anchor='ack'>
<p>Thanks to Waqas Hussain for implementation feedback.</p>
</section1>
</xep>