replace msgpack-python with u-msgpack; remove forcing dict keys being unicode in general
This commit is contained in:
parent
99f723f64b
commit
9e00896d36
@ -243,11 +243,12 @@ class JsonSerializer(Serializer):
|
||||
ISerializer.register(JsonSerializer)
|
||||
|
||||
|
||||
#
|
||||
# MsgPack serialization depends on the `msgpack` package being available
|
||||
# MsgPack serialization depends on the `u-msgpack` package being available
|
||||
# https://pypi.python.org/pypi/u-msgpack-python
|
||||
# https://github.com/vsergeev/u-msgpack-python
|
||||
#
|
||||
try:
|
||||
import msgpack
|
||||
import umsgpack
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
@ -259,15 +260,8 @@ else:
|
||||
Flag that indicates whether this serializer needs a binary clean transport.
|
||||
"""
|
||||
|
||||
ENABLE_V5 = True
|
||||
"""
|
||||
Enable version 5 of the MsgPack specification (which differentiates
|
||||
between strings and binary).
|
||||
"""
|
||||
|
||||
def __init__(self, batched=False):
|
||||
"""
|
||||
Ctor.
|
||||
|
||||
:param batched: Flag that controls whether serializer operates in batched mode.
|
||||
:type batched: bool
|
||||
@ -278,7 +272,7 @@ else:
|
||||
"""
|
||||
Implements :func:`autobahn.wamp.interfaces.IObjectSerializer.serialize`
|
||||
"""
|
||||
data = msgpack.packb(obj, use_bin_type=self.ENABLE_V5)
|
||||
data = umsgpack.packb(obj)
|
||||
if self._batched:
|
||||
return struct.pack("!L", len(data)) + data
|
||||
else:
|
||||
@ -289,22 +283,6 @@ else:
|
||||
Implements :func:`autobahn.wamp.interfaces.IObjectSerializer.unserialize`
|
||||
"""
|
||||
|
||||
def ensure_string_keys(d):
|
||||
"""
|
||||
under python 2, with use_bin_type=True, most dict keys end up
|
||||
getting encoded as bytes (any syntax except {u"key":
|
||||
u"value"}) so instead of recursively looking through
|
||||
everything that's getting serialized, we fix them up
|
||||
on the way out using msgpack's `object_hook` as
|
||||
there's no corresponding hook for serialization...
|
||||
"""
|
||||
for (k, v) in six.iteritems(d):
|
||||
if not isinstance(k, six.text_type):
|
||||
newk = six.text_type(k, encoding='utf8')
|
||||
del d[k]
|
||||
d[newk] = v
|
||||
return d
|
||||
|
||||
if self._batched:
|
||||
msgs = []
|
||||
N = len(payload)
|
||||
@ -321,13 +299,7 @@ else:
|
||||
data = payload[i + 4:i + 4 + l]
|
||||
|
||||
# append parsed raw message
|
||||
msgs.append(
|
||||
msgpack.unpackb(
|
||||
data,
|
||||
encoding='utf-8',
|
||||
object_hook=ensure_string_keys,
|
||||
)
|
||||
)
|
||||
msgs.append(umsgpack.unpackb(data))
|
||||
|
||||
# advance until everything consumed
|
||||
i = i + 4 + l
|
||||
@ -337,11 +309,7 @@ else:
|
||||
return msgs
|
||||
|
||||
else:
|
||||
unpacked = msgpack.unpackb(
|
||||
payload,
|
||||
encoding='utf-8',
|
||||
object_hook=ensure_string_keys,
|
||||
)
|
||||
unpacked = umsgpack.unpackb(payload)
|
||||
return [unpacked]
|
||||
|
||||
IObjectSerializer.register(MsgPackObjectSerializer)
|
||||
|
@ -27,7 +27,6 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
import unittest2 as unittest
|
||||
import six
|
||||
|
||||
from autobahn.wamp import message
|
||||
from autobahn.wamp import role
|
||||
@ -103,50 +102,6 @@ class TestSerializer(unittest.TestCase):
|
||||
self.serializers.append(serializer.UBJSONSerializer())
|
||||
self.serializers.append(serializer.UBJSONSerializer(batched=True))
|
||||
|
||||
def test_dict_keys_msgpack(self):
|
||||
"""
|
||||
dict keys should always be strings. the data provided is from
|
||||
calling msgpack encode on a dict in python2 with
|
||||
`use_bin_type=True` and the following message:
|
||||
|
||||
print(ser.serialize(
|
||||
message.Call(
|
||||
123456, u"com.myapp.procedure1",
|
||||
args=(),
|
||||
kwargs={u'unicode': 23, 'str': 42}
|
||||
)
|
||||
))
|
||||
"""
|
||||
|
||||
if not hasattr(serializer, 'MsgPackSerializer'):
|
||||
self.skipTest("no msgpack")
|
||||
|
||||
ser = serializer.MsgPackSerializer()
|
||||
payload = b'\x960\xce\x00\x01\xe2@\x80\xb4com.myapp.procedure1\x90\x82\xc4\x03str*\xa7unicode\x17'
|
||||
msg_out = ser.unserialize(payload, True)[0]
|
||||
|
||||
for k in msg_out.kwargs.keys():
|
||||
self.assertEqual(type(k), six.text_type)
|
||||
self.assertTrue('str' in msg_out.kwargs)
|
||||
self.assertTrue('unicode' in msg_out.kwargs)
|
||||
|
||||
def test_dict_keys_msgpack_batched(self):
|
||||
"""
|
||||
dict keys should always be strings. the data provided is from
|
||||
calling msgpack encode on a dict in python2 with
|
||||
`use_bin_type=True`
|
||||
"""
|
||||
if not hasattr(serializer, 'MsgPackSerializer'):
|
||||
self.skipTest("no msgpack")
|
||||
|
||||
ser = serializer.MsgPackSerializer(batched=True)
|
||||
payload = b'\x00\x00\x00-\x960\xce\x00\x01\xe2@\x80\xb4com.myapp.procedure1\x90\x82\xa7unicode\x17\xa3str*'
|
||||
msg_out = ser.unserialize(payload, True)[0]
|
||||
for k in msg_out.kwargs.keys():
|
||||
self.assertEqual(type(k), six.text_type)
|
||||
self.assertTrue('str' in msg_out.kwargs)
|
||||
self.assertTrue('unicode' in msg_out.kwargs)
|
||||
|
||||
def test_roundtrip(self):
|
||||
msgs = generate_test_messages()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user