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 (XEP-0239) [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:
Given the need to include the namespaces within the JSON, an immediately obvious structure may be something like::
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:
To use this improved encoding (eminently suitable both for c2s and s2s connections), entities should follow the connection rules defined in XMPP Core [2] 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:
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 {}:
are considered to be more offensive than <>=
the legacy XML encoding may be preferred.
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.
Thanks to Waqas Hussain for implementation feedback.
This document in other formats: XML PDF
This XMPP Extension Protocol is copyright © 1999 – 2024 by the XMPP Standards Foundation (XSF).
Permission is hereby granted, free of charge, to any person obtaining a copy of this specification (the "Specification"), 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.
## NOTE WELL: This Specification is provided on an "AS IS" 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 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 from, out of, or in connection with the Specification or the implementation, deployment, or other use of 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.
This XMPP Extension Protocol has been contributed in full conformance with the XSF's Intellectual Property Rights Policy (a copy of which can be found at <https://xmpp.org/about/xsf/ipr-policy> or obtained by writing to XMPP Standards Foundation, P.O. Box 787, Parker, CO 80134 USA).
The HTML representation (you are looking at) is maintained by the XSF. It is based on the YAML CSS Framework, which is licensed under the terms of the CC-BY-SA 2.0 license.
The Extensible Messaging and Presence Protocol (XMPP) is defined in the XMPP Core (RFC 6120) and XMPP IM (RFC 6121) 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 <https://xmpp.org/community/> 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 <https://xmpp.org/extensions/xep-0239.html>.
2. 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 https://xmpp.org/extensions/attic/
April Fools!
@report{smith2011n/a, title = {JSON Encodings for XMPP}, author = {Smith, Kevin and Wild, Matthew}, type = {XEP}, number = {0295}, version = {1.0}, institution = {XMPP Standards Foundation}, url = {https://xmpp.org/extensions/xep-0295.html}, date = {2011-04-01/2011-04-01}, }
END