From 5cd038c8918e12d2bb752f8cebb150cb0b2ad1a0 Mon Sep 17 00:00:00 2001 From: Jeff Pinner Date: Tue, 17 Oct 2017 15:07:14 -0700 Subject: [PATCH] 1.5.4 -- patch release to prepare for pull request #158 in 1.6.0 --- pynamodb/__init__.py | 2 +- pynamodb/attributes.py | 18 ++++++++++++++++++ pynamodb/tests/test_attributes.py | 17 +++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pynamodb/__init__.py b/pynamodb/__init__.py index 17b87a403..d3db1f949 100644 --- a/pynamodb/__init__.py +++ b/pynamodb/__init__.py @@ -7,4 +7,4 @@ """ __author__ = 'Jharrod LaFon' __license__ = 'MIT' -__version__ = '1.5.3' +__version__ = '1.5.4' diff --git a/pynamodb/attributes.py b/pynamodb/attributes.py index d5d44c033..3f6e2c717 100644 --- a/pynamodb/attributes.py +++ b/pynamodb/attributes.py @@ -140,6 +140,24 @@ class UnicodeSetAttribute(SetMixin, Attribute): attr_type = STRING_SET null = True + def element_deserialize(self, value): + """ + This prepares for a future serialization change in v1.6.0 which will stop + serializing json encoded strings. This method attempts to deserialize a json + encoded value and falls back to returning the raw value if json decoding fails. + """ + result = value + try: + result = json.loads(value) + except ValueError: + # it's serialized in the new way so pass + pass + return result + + def deserialize(self, value): + if value and len(value): + return set([self.element_deserialize(val) for val in value]) + class UnicodeAttribute(Attribute): """ diff --git a/pynamodb/tests/test_attributes.py b/pynamodb/tests/test_attributes.py index 8e334815f..bbf405135 100644 --- a/pynamodb/tests/test_attributes.py +++ b/pynamodb/tests/test_attributes.py @@ -353,9 +353,22 @@ def test_unicode_set_deserialize(self): UnicodeSetAttribute.deserialize """ attr = UnicodeSetAttribute() + value = set([six.u('foo'), six.u('bar')]) self.assertEqual( - attr.deserialize([json.dumps(val) for val in sorted(set([six.u('foo'), six.u('bar')]))]), - set([six.u('foo'), six.u('bar')]) + attr.deserialize(value), + value + ) + + def test_unicode_set_deserialize_json(self): + """ + UnicodeSetAttribute.deserialize old way + """ + attr = UnicodeSetAttribute() + value = set([six.u('foo'), six.u('bar')]) + old_value = set([json.dumps(val) for val in value]) + self.assertEqual( + attr.deserialize(old_value), + value ) def test_unicode_set_attribute(self):