Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE xep SYSTEM 'xep.dtd' [
  3. <!ENTITY % ents SYSTEM 'xep.ent'>
  4. %ents;
  5. ]>
  6. <?xml-stylesheet type='text/xsl' href='xep.xsl'?>
  7. <xep>
  8. <header>
  9. <title>JSON Encodings for XMPP</title>
  10. <abstract>This specification defines an alternative JSON encoding for XMPP stanzas and other elements.</abstract>
  11. &LEGALNOTICE;
  12. <number>0295</number>
  13. <status>Active</status>
  14. <type>Humorous</type>
  15. <sig>Standards</sig>
  16. <approver>Council</approver>
  17. <dependencies>
  18. <spec>XMPP Core</spec>
  19. </dependencies>
  20. <supersedes/>
  21. <supersededby><spec>XEP-0335</spec></supersededby>
  22. <shortname>N/A</shortname>
  23. &ksmith;
  24. &mwild;
  25. <revision>
  26. <version>1.0</version>
  27. <date>2011-04-01</date>
  28. <initials>ks, mw</initials>
  29. <remark><p>April Fools!</p></remark>
  30. </revision>
  31. </header>
  32. <section1 topic='Introduction' anchor='intro'>
  33. <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>
  34. <p></p>
  35. </section1>
  36. <section1 topic='Protocol' anchor='protocol'>
  37. <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>
  38. <p>Let us consider a fairly standard XMPP message stanza:</p>
  39. <example caption='XML-encoded XMPP stanza'><![CDATA[
  40. <message to="alice@wonderland.lit" id="if00lu" >
  41. <body>Hi you</body>
  42. <body xml:lang="cy">Prynhawn da</body>
  43. <nick xmlns="http://jabber.org/protocol/nick">Alice</nick>
  44. <active xmlns="http://jabber.org/protocol/chatstates"/>
  45. </message>
  46. ]]></example>
  47. <p>Given the need to include the namespaces within the JSON, an immediately obvious structure may be something like::</p>
  48. <example caption='Na&#239;ve JSON representation'><![CDATA[
  49. {"stanza-name": "message",
  50. "stanza-namespace": "jabber:client",
  51. "stanza-attributes": {
  52. "to":"alice@wonderland.lit",
  53. "id":"if00lu"
  54. }
  55. "stanza-children": [
  56. {
  57. "element-name": "body",
  58. "element-namespace": "jabber:client",
  59. "element-value": "Hi you"
  60. },
  61. {
  62. "element-name": "body",
  63. "element-namespace": "jabber:client",
  64. "element-language": "cy",
  65. "element-value": "Prynhawn da"
  66. },
  67. {
  68. "element-name": "nick",
  69. "element-namespace": "http://jabber.org/protocol/nick",
  70. "element-value": "Alice"
  71. },
  72. {
  73. "element-name": "active",
  74. "element-namespace": "http://jabber.org/protocol/chatstates",
  75. }
  76. ]
  77. }
  78. ]]></example>
  79. <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>
  80. <example caption='Advanced JSON-encoding'><![CDATA[
  81. {"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>"}
  82. ]]></example>
  83. <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>
  84. </section1>
  85. <section1 topic='Examples' anchor='examples'>
  86. <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>
  87. <example caption='XML-encoded Message with Security Label'><![CDATA[
  88. <message to='romeo@example.net' from='juliet@example.com/balcony'>
  89. <body>This content is classified.</body>
  90. <securitylabel xmlns='urn:xmpp:sec-label:0'>
  91. <displaymarking fgcolor='black' bgcolor='red'>SECRET</displaymarking>
  92. <label><esssecuritylabel xmlns='urn:xmpp:sec-label:ess:0'>
  93. MQYCAQQGASk=
  94. </esssecuritylabel></label>
  95. </securitylabel>
  96. </message>
  97. ]]></example>
  98. <example caption='JSON-encoded Message with Security Label'><![CDATA[
  99. {"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>"}
  100. ]]></example>
  101. <example caption='XML-encoded XHTML-IM message'><![CDATA[
  102. <message from='ladymacbeth@shakespeare.lit/castle'
  103. to='macbeth@chat.shakespeare.lit'
  104. type='groupchat'>
  105. <body>Yet here's a spot.</body>
  106. <html xmlns='http://jabber.org/protocol/xhtml-im'>
  107. <body xmlns='http://www.w3.org/1999/xhtml'>
  108. <p>
  109. Yet here's a spot.
  110. <img alt='A spot'
  111. src='cid:sha1+8f35fef110ffc5df08d579a50083ff9308fb6242@bob.xmpp.org'/>
  112. </p>
  113. </body>
  114. </html>
  115. </message>
  116. ]]></example>
  117. <example caption='JSON-encoded XHTML-IM message'><![CDATA[
  118. {"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>"}
  119. ]]></example>
  120. <example caption='XML-encoded Dialback Key Transmission'><![CDATA[
  121. <db:result
  122. from='sender.tld'
  123. to='target.tld'>
  124. 1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9
  125. </db:result>
  126. ]]></example>
  127. <example caption='JSON-encoded Dialback Key Transmission'><![CDATA[
  128. {"s":"<db:result from='sender.tld' to='target.tld'>1e701f120f66824b57303384e83b51feba858024fd2221d39f7acc52dcf767a9</db:result>"}
  129. ]]></example>
  130. <example caption='XML-encoded Event Publication'><![CDATA[
  131. <iq from='juliet@capulet.lit/balcony' type='set' id='pub1'>
  132. <pubsub xmlns='http://jabber.org/protocol/pubsub'>
  133. <publish node='http://jabber.org/protocol/tune'>
  134. <item>
  135. <tune xmlns='http://jabber.org/protocol/tune'>
  136. <artist>Gerald Finzi</artist>
  137. <length>255</length>
  138. <source>Music for 'Love's Labors Lost' (Suite for small orchestra)</source>
  139. <title>Introduction (Allegro vigoroso)</title>
  140. <track>1</track>
  141. </tune>
  142. </item>
  143. </publish>
  144. </pubsub>
  145. </iq>
  146. ]]></example>
  147. <example caption='JSON-encoded Event Publication'><![CDATA[
  148. {"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>"}
  149. ]]></example>
  150. <example caption='XML-encoded Stream Header'><![CDATA[
  151. <stream:stream
  152. from='juliet@example.com'
  153. to='example.com'
  154. version='1.0'
  155. xml:lang='en'
  156. xmlns='jabber:client'
  157. xmlns:stream='http://etherx.jabber.org/streams'>
  158. ]]></example>
  159. <example caption='JSON-encoded Stream Header'><![CDATA[
  160. {"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'>"}
  161. ]]></example>
  162. <p>Beautiful, elegant <em>and</em> efficient at the same time.</p>
  163. </section1>
  164. <section1 topic='Internationalization Considerations' anchor='i18n'>
  165. <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>
  166. </section1>
  167. <section1 topic='Security Considerations' anchor='security'>
  168. <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>
  169. </section1>
  170. <section1 topic='IANA Considerations' anchor='iana'>
  171. <p>None.</p>
  172. </section1>
  173. <section1 topic='XMPP Registrar Considerations' anchor='registrar'>
  174. <p>The XMPP Registrar may wish to consider maintenance of dual registries - for both XML and JSON encodings, but this is OPTIONAL.</p>
  175. </section1>
  176. <section1 topic='JSON Schema' anchor='schema'>
  177. <code><![CDATA[
  178. {
  179. "type": "object",
  180. "additionalProperties": false,
  181. "properties": {
  182. "s": {
  183. "type": "string",
  184. "required": true,
  185. "format": "xml"
  186. }
  187. }
  188. }
  189. ]]></code>
  190. </section1>
  191. <section1 topic='Acknowledgements' anchor='ack'>
  192. <p>Thanks to Waqas Hussain for implementation feedback.</p>
  193. </section1>
  194. </xep>