SleekXMPP
SleekXMPP copied to clipboard
support for json containers
Hey man, this is awesome and should definitely get merged. I'm new to SleekXMPP (xmpp in general) and looking to send some data via a JSON container within a message stanza. This commit will allow me to do that? Any example code?
@jstoxrocky I hope to get this merged too, however, I am not 100% compliant with XEP_0335.
This plugin places the json into an attribute of the json tag. Here is some example code. Make sure to register the plugin of course.
msg['json'] = {key: value}
ends up looking like
<message> <body> some body </body><json xmlns="urnsomethingsomething" json='{key: value'}/>
instead of
<message><body> some body </body> <json xmlns="urnsomethingsomething>'{key: value'}</json>
You could go ahead and use it right now by manually placing my module in your plugins directory. It's that easy.
@ryhamz That's awesome! Thanks. Is there a reason why you did not follow 100% compliancy with XEP_0335? I'm actually looking to communicate with a server expecting json data as the text contents of the json container, instead of an attribute in the XML. Thoughts on how this could be expanded to include this?
@jstoxrocky I wanted to follow it, but could not figure it out. I figured a PR here would get more eyes on it. Someone (in sleekxmpp conference chat) suggested overriding set_attr to set the inner text. If you could modify this to be compliant, I would be excited.
For now, this is a wart in my system.
So I did some playing around and got compliancy with XEP_0335 with a few tweaks to your code.
If we remove lines 9 and 10 from sleekxmpp/plugins/xep_0335/stanza.py
class JsonContainers(ElementBase):
name = 'json'
namespace = 'urn:xmpp:json:0'
plugin_attrib = 'json'
Then we can do something like:
from sleekxmpp.plugins.xep_0335 import JsonContainers
my_stanza = JsonContainers()
my_stanza.xml.text = "Value"
print my_stanza
Giving us:
<json xmlns="urn:xmpp:json:0">Value</json>
Or alternatively a nested version,
from sleekxmpp.plugins.xep_0335 import JsonContainers
from sleekxmpp.xmlstream import register_stanza_plugin
from sleekxmpp.stanza import Message
register_stanza_plugin(Message, JsonContainers)
msg = Message()
msg['json'].xml.text = 'Value'
print msg
Giving us:
<message xmlns="jabber:client"><json xmlns="urn:xmpp:json:0">Value</json></message>
Are lines 9 and 10 necessary? I think 9 isn't needed to get the JSON values as the XML text, and I'm not sure of the point of 10.