Skip to content

Commit

Permalink
Add tests and support in pure python for encoding dicts with int keys…
Browse files Browse the repository at this point in the history
… as strings
  • Loading branch information
njoyce committed Nov 25, 2010
1 parent aec0bd2 commit fba182a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyamf/amf0.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import datetime

import pyamf
from pyamf import util, codec, xml
from pyamf import util, codec, xml, python


#: Represented as 9 bytes: 1 byte for C{0x00} and 8 bytes a double
Expand Down Expand Up @@ -533,6 +533,9 @@ def _writeDict(self, o):
@param o: The C{dict} data to be encoded to the AMF0 data stream.
"""
for key, val in o.iteritems():
if type(key) in python.int_types:
key = str(key)

self.serialiseString(key)
self.writeElement(val)

Expand Down
3 changes: 3 additions & 0 deletions pyamf/amf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,9 @@ def writeObject(self, obj, is_proxy=False):
if definition.encoding == ObjectEncoding.DYNAMIC:
if attrs:
for attr, value in attrs.iteritems():
if type(attr) in python.int_types:
attr = str(attr)

self.serialiseString(attr)
self.writeElement(value)

Expand Down
5 changes: 5 additions & 0 deletions pyamf/tests/test_amf0.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def test_longstring(self):
def test_dict(self):
self.assertEncoded({'a': 'a'}, '\x03\x00\x01a\x02\x00\x01a\x00\x00\t')

self.assertEncoded({12: True, 42: "Testing"}, '\x03', (
'\x00\x0242\x02\x00\x07Testing',
'\x00\x0212\x01\x01'
), '\x00\x00\t')

def test_mixed_array(self):
d = pyamf.MixedArray(a=1, b=2, c=3)

Expand Down
4 changes: 4 additions & 0 deletions pyamf/tests/test_amf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ def test_dict(self):
self.assertEncoded({'a': u'e', 'b': u'f', 'c': u'g', 'd': u'h'},
'\n\x0b\x01', ('\x03c\x06\x03g', '\x03b\x06\x03f', '\x03a\x06\x03e',
'\x03d\x06\x03h'), '\x01')
self.assertEncoded({12: True, 42: "Testing"}, ('\n\x0b', (
'\x01\x0542\x06\x0fTesting',
'\x0512\x03\x01'
)))

def test_boolean(self):
self.assertEncoded(True, '\x03')
Expand Down

0 comments on commit fba182a

Please sign in to comment.