Commits

Anonymous committed b3759b6 Draft

marshal/unmarshal with jsonpickle

Comments (0)

Files changed (4)

araldo/marshalling/default_marshalling.py

+""" Default implemenation of marshalling/unmarshalling
+"""
 import json
+import jsonpickle
+from datetime import datetime
 from araldo.marshalling import MarshallingBase
 from araldo.message import Message
 
+JSONPICKLE_TIMESTAMP_FORMAT="%Y-%m-%d_%H:%M:%S.%f"
+
 class DefaultMarshalling(MarshallingBase):
+    """ Marhals/unmarshals to/from JSON
+    """
     def __init__(self):
         MarshallingBase.__init__(self)
 
     def to_internal_format(self, message_str):
         obj =  json.loads(message_str)
+        # fix date
+        obj["creation_timestamp"] = datetime.strptime(
+            obj["creation_timestamp"], JSONPICKLE_TIMESTAMP_FORMAT)
         return Message(**obj)
 
     def to_external_format(self, message):
-        msg_data =  {
-            "msg_id": message.msg_id
-        }
-        return json.dumps(msg_data)
+        """ Serializes to JSON
+        """
+        return jsonpickle.encode(message, unpicklable=False)

araldo/message.py

                  origin, destination,
                  payload, msg_id=None,
                  creation_timestamp=None):
-        self._origin = origin
-        self._destination = destination
-        self._payload = payload
-        self._msg_id = msg_id or Message._create_msg_id()
-        self._creation_timestamp = creation_timestamp or datetime.now()
-
-    @property
-    def msg_id(self):
-        """ :return: message ID
-        """
-        return self._msg_id
-
-    @property
-    def payload(self):
-        """ :return: message contents
-        """
-        return self._payload
-
-    @property
-    def creation_timestamp(self):
-        """ :return: timestamp of message creation
-        """
-        return self._creation_timestamp
+        self.origin = origin
+        self.destination = destination
+        self.payload = payload
+        self.msg_id = msg_id or Message._create_msg_id()
+        self.creation_timestamp = creation_timestamp or datetime.now()
 
     @staticmethod
     def _create_msg_id():
 
     def __unicode__(self):
         return "$[%s]<%s->%s>:%s" % (
-            self._msg_id,
-            self._origin,
-            self._destination,
-            self._payload)
+            self.msg_id,
+            self.origin,
+            self.destination,
+            self.payload)
         'gevent-websocket >= 0.3.6',
         'websocket_client >= 0.8.0',
         'pyYAML >= 3.10',
-        'voluptuous >= 0.4'
+        'voluptuous >= 0.4',
+        'jsonpickle >= 0.4.0'
     ],
     entry_points={
         "araldo.endpoints": [

test/marshalling_test.py

 import pytest
 import json
 from datetime import datetime
-from araldo.marshalling.default_marshalling import DefaultMarshalling
+from araldo.marshalling.default_marshalling import(
+    DefaultMarshalling, JSONPICKLE_TIMESTAMP_FORMAT)
 from araldo.message import Message
 
-TIMESTAMP_FORMAT = "%Y%m%d_%H%M%S"
+#TIMESTAMP_FORMAT = "%Y%m%d_%H%M%S"
+
+
+@pytest.fixture
+def timestamp():
+    return datetime(2012, 10, 1)
+
+
+@pytest.fixture
+def timestamp_str(timestamp):
+    return unicode(timestamp.strftime(JSONPICKLE_TIMESTAMP_FORMAT))
 
 
 @pytest.fixture
 
 
 @pytest.fixture
-def message_str():
-    message_obj = {
-        "origin": "test_origin",
-        "destination": "test_destination",
-        "payload": "test_payload",
-        "msg_id": "test_msg_id",
-        "creation_timestamp": datetime(2012, 10, 1).strftime(TIMESTAMP_FORMAT)}
+def message_obj(timestamp_str):
+    return  {
+        u"origin": u"test_origin",
+        u"destination": u"test_destination",
+        u"payload": u"test_payload",
+        u"msg_id": u"test_msg_id",
+        u"creation_timestamp": timestamp_str}
+
+
+@pytest.fixture
+def message_str(message_obj):
     return json.dumps(message_obj)
 
 
 @pytest.fixture
-def message():
+def message(timestamp):
     return Message(
         origin="test_origin",
         destination="test_destination",
         payload="test_payload",
         msg_id="test_msg_id",
-        creation_timestamp=datetime(2012, 10, 1).strftime(TIMESTAMP_FORMAT)
+        creation_timestamp=timestamp
     )
 
 
     pass
 
 
-def test_to_internal_format(default_marshalling, message_str):
+def test_to_internal_format(default_marshalling, message_str, timestamp):
     message = default_marshalling.to_internal_format(message_str)
     assert isinstance(message, Message)
+    assert message.origin == "test_origin"
+    assert message.destination == "test_destination"
     assert message.msg_id == "test_msg_id"
     assert message.payload == "test_payload"
+    assert message.creation_timestamp == timestamp
 
 
-def test_to_external_format(default_marshalling, message):
+def test_to_external_format(default_marshalling, message, message_str):
     message_str = default_marshalling.to_external_format(message)
     assert type(message_str) == str
-    json.loads(message_str)
+    decoded = json.loads(message_str)
+    assert decoded["origin"] == message.origin
+    assert decoded["destination"] == message.destination
+    assert decoded["payload"] == message.payload
+    assert decoded["msg_id"] == message.msg_id
+    assert decoded["creation_timestamp"] == u"2012-10-01 00:00:00"