Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

support for json containers#433

Open
ryhamz wants to merge 1 commit into
fritzy:developfrom
ryhamz:develop
Open

support for json containers#433
ryhamz wants to merge 1 commit into
fritzy:developfrom
ryhamz:develop

Conversation

@ryhamz

@ryhamz ryhamz commented Nov 8, 2016

Copy link
Copy Markdown

No description provided.

@jstoxrocky

Copy link
Copy Markdown

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?

@ryhamz

ryhamz commented Dec 19, 2016

Copy link
Copy Markdown
Author

@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.

@jstoxrocky

Copy link
Copy Markdown

@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?

@ryhamz

ryhamz commented Dec 20, 2016

Copy link
Copy Markdown
Author

@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.

@jstoxrocky

jstoxrocky commented Dec 21, 2016

Copy link
Copy Markdown

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants