Skip to content

Commit

Permalink
Merge pull request #13 from 4teamwork/jone-persistent-obj-encoding
Browse files Browse the repository at this point in the history
Brain serializer: fix JSON error when there persistent mutables in the catalog
  • Loading branch information
jone committed Mar 24, 2015
2 parents 75023da + 5d816b3 commit 7ddb909
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/HISTORY.txt
Expand Up @@ -5,7 +5,8 @@ Changelog
1.0.10 (unreleased)
-------------------

- Nothing changed yet.
- Brain serializer: fix JSON error when there persistent mutables in the catalog.
[jone]


1.0.9 (2014-08-20)
Expand Down
19 changes: 17 additions & 2 deletions ftw/bridge/client/brain.py
@@ -1,10 +1,12 @@
import DateTime
from ftw.bridge.client.interfaces import IBrainRepresentation
from ftw.bridge.client.interfaces import IBrainSerializer
from ftw.bridge.client.utils import get_brain_url
from persistent.list import PersistentList
from persistent.mapping import PersistentMapping
from Products.CMFCore.utils import getToolByName
from zope.component.hooks import getSite
from zope.interface import implements
import DateTime
import Missing


Expand Down Expand Up @@ -48,7 +50,20 @@ def _encode(self, value):
return [':DateTime', str(value)]

elif isinstance(value, tuple):
return list(value)
return self._encode(list(value))

elif isinstance(value, PersistentMapping):
return self._encode(dict(value))

elif isinstance(value, dict):
return dict((self._encode(key), self._encode(value))
for key, value in value.items())

elif isinstance(value, PersistentList):
return self._encode(list(value))

elif isinstance(value, list):
return map(self._encode, value)

return value

Expand Down
18 changes: 18 additions & 0 deletions ftw/bridge/client/tests/test_brain.py
Expand Up @@ -6,6 +6,8 @@
from ftw.bridge.client.testing import INTEGRATION_TESTING
from ftw.builder import Builder
from ftw.builder import create
from persistent.list import PersistentList
from persistent.mapping import PersistentMapping
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from Products.CMFCore.utils import getToolByName
Expand Down Expand Up @@ -111,6 +113,22 @@ def test_deserializing(self):
self.assertEqual('Folder', folder.portal_type)
self.assertEqual('Document', page.portal_type)

def test_converts_persistent_mutables_to_default_types(self):
# On the remote side the data is not stored, therfore we can
# convert persistent mutables to default types in order to be
# able to dump it to json.

serializer = BrainSerializer()
self.assertEquals(dict, type(serializer._encode(PersistentMapping())),
'PersistentMapping should be converted to dict')
self.assertEquals(list, type(serializer._encode(PersistentList())),
'PersistentList should be converted to list')

self.assertEquals(dict, type(serializer._encode({'dict': PersistentMapping()})['dict']),
'Dicts should be encoded recursively')
self.assertEquals(list, type(serializer._encode([PersistentList()])[0]),
'Lists should be encoded recursively')


class TestBrainResultSet(TestCase):

Expand Down

0 comments on commit 7ddb909

Please sign in to comment.