| Abstract: | This specification defines an alternative JSON encoding for XMPP stanzas and other elements. |
| Authors: | Kevin Smith, Matthew Wild |
| Copyright: | © 1999 - 2011 XMPP Standards Foundation. SEE LEGAL NOTICES. |
| Status: | Active |
| Type: | Humorous |
| Version: | 1.0 |
| Last Updated: | 2011-04-01 |
NOTICE: This document is Humorous. It MAY provide amusement but SHOULD NOT be taken seriously.
1. Introduction
2. Protocol
3. Examples
4. Internationalization Considerations
5. Security Considerations
6. IANA Considerations
7. XMPP Registrar Considerations
8. JSON Schema
9. Acknowledgements
Appendices
A: Document Information
B: Author Information
C: Legal Notices
D: Relation to XMPP
E: Discussion Venue
F: Requirements Conformance
G: Notes
H: Revision History
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 Binary XMPP [1]. 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.
The recently updated XMPP Core [2] 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.
Let us consider a fairly standard XMPP message stanza:
Example 1. XML-encoded XMPP stanza
<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>
Given the need to include the namespaces within the JSON, an immediately obvious structure may be something like::
Example 2. Naïve JSON representation
{"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",
}
]
}
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:
Example 3. Advanced JSON-encoding
{"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>"}
To use this improved encoding (eminently suitable both for c2s and s2s connections), entities should follow the connection rules defined in XMPP Core [3] 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.
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:
Example 4. XML-encoded Message with Security Label
<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 5. JSON-encoded Message with Security Label
{"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 6. XML-encoded XHTML-IM message
<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 7. JSON-encoded XHTML-IM message
{"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 8. XML-encoded Dialback Key Transmission
<db:result
from='sender.tld'
to='target.tld'>
1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9
</db:result>
Example 9. JSON-encoded Dialback Key Transmission
{"s":"<db:result from='sender.tld' to='target.tld'>1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9</db:result>"}
Example 10. XML-encoded Event Publication
<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 11. JSON-encoded Event Publication
{"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 12. XML-encoded Stream Header
<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 13. JSON-encoded Stream Header
{"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'>"}
Beautiful, elegant and efficient at the same time.
It is hoped that this representation introduces no new internationalization considerations, although it is acknowledged that if there are cultures where the symbols
{}:<>=
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.
None.
The XMPP Registrar may wish to consider maintenance of dual registries - for both XML and JSON encodings, but this is OPTIONAL.
{
"type": "object",
"additionalProperties": false,
"properties": {
"s": {
"type": "string",
"required": true,
"format": "xml"
}
}
}
Thanks to Waqas Hussain for implementation feedback.
Series: XEP
Number: 0295
Publisher: XMPP Standards Foundation
Status:
Active
Type:
Humorous
Version: 1.0
Last Updated: 2011-04-01
Approving Body: XMPP Council
Dependencies: XMPP Core
Supersedes: None
Superseded By: None
Short Name: N/A
Source Control:
HTML
This document in other formats:
XML
PDF
Email:
kevin@kismith.co.uk
JabberID:
kevin@doomsong.co.uk
Email:
mwild1@gmail.com
JabberID:
me@matthewwild.co.uk
The Extensible Messaging and Presence Protocol (XMPP) is defined in the XMPP Core (RFC 3920) and XMPP IM (RFC 3921) specifications contributed by the XMPP Standards Foundation to the Internet Standards Process, which is managed by the Internet Engineering Task Force in accordance with RFC 2026. Any protocol defined in this document has been developed outside the Internet Standards Process and is to be understood as an extension to XMPP rather than as an evolution, development, or modification of XMPP itself.
The primary venue for discussion of XMPP Extension Protocols is the <standards@xmpp.org> discussion list.
Discussion on other xmpp.org discussion lists might also be appropriate; see <http://xmpp.org/about/discuss.shtml> for a complete list.
Errata can be sent to <editor@xmpp.org>.
The following requirements keywords as used in this document are to be interpreted as described in RFC 2119: "MUST", "SHALL", "REQUIRED"; "MUST NOT", "SHALL NOT"; "SHOULD", "RECOMMENDED"; "SHOULD NOT", "NOT RECOMMENDED"; "MAY", "OPTIONAL".
1. XEP-0239: Binary XMPP <http://xmpp.org/extensions/xep-0239.html>.
2. RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core <http://tools.ietf.org/html/rfc6120>.
3. RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core <http://tools.ietf.org/html/rfc6120>.
Note: Older versions of this specification might be available at http://xmpp.org/extensions/attic/
April Fools!
(ks, mw)END