Kyle Schaffrick avatar Kyle Schaffrick committed ed66f7a

Add JSONMessage and usage example.

Comments (0)

Files changed (3)

                 return
             else:
                 yield msg
+
+
+try:
+    from jsonmessage import JSONMessage
+except ImportError:
+    pass

amqpev/jsonmessage.py

+# Copyright (c) 2009 Kyle Schaffrick
+#
+# This module is part of AMQPev and is released under the MIT license. See
+# COPYING in the root of this distribution for details.
+
+from api import Message
+import simplejson
+
+class JSONMessage(Message):
+    def __init__(self, body, **kwargs):
+        kwargs['content_type'] = 'application/json'
+        Message.__init__(self, body, **kwargs)
+
+    def encode(self):
+        return simplejson.JSONEncoder().encode(self.body)
+
+    @classmethod
+    def decode(cls, encoded_body, properties):
+        return cls(simplejson.JSONDecoder().decode(encoded_body), **properties)
+
+Message.add_content_type('application/json', JSONMessage)

examples/json_message.py

+from __future__ import with_statement
+
+import sys
+from pprint import pprint
+
+import amqpev.api as mq
+import eventlet.api as ev
+from eventlet.coros import metaphore
+
+
+def main():
+
+    service_factory = mq.BrokerServiceFactory('mq.local')
+
+    try:
+        round_trip(service_factory)
+    finally:
+        service_factory.close()
+
+
+def round_trip(svc_f):
+    with svc_f() as svc:
+        json_q = svc.queue()
+        json_q.publish(mq.JSONMessage(sys.path))
+
+        with svc.consume_from(json_q) as cs:
+            message = cs.next_message()
+            pprint(message.body)
+
+if __name__ == '__main__':
+    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.