Commits

Jason R. Coombs committed 69684a7

Fixed decoding of nested items

  • Participants
  • Parent commits 85849e3

Comments (0)

Files changed (2)

+>>> from jaraco.json.main import *
+>>> from jaraco.json.test_encoding import Satchel, Marble
+>>> s = Satchel()
+>>> s.add(Marble('blue'))
+>>> import pymongo
+>>> db = pymongo.Connection().stuff
+>>> db.satchels.drop()
+>>> db.satchels.insert(GenericEncoder().default(s)) # doctest: +ELLIPSIS
+ObjectId(...)
+>>> data = db.satchels.find_one()
+>>> res = decode_object_hook(data)
+>>> res.items[0].color
+u'blue'
+
+You can even query the data
+
+>>> data = db.satchels.find_one({'state.items.state.color': 'blue'})
+>>> res = decode_object_hook(data)

jaraco/json/main.py

 	return parent
 
 def decode_object_hook(object):
+	doh = decode_object_hook
+	if isinstance(object, (list, tuple)):
+		return map(doh, object)
+	if not isinstance(object, dict):
+		return object
 	if '__python_class__' not in object.keys():
 		return object
 	class_name = object['__python_class__']
 	mod_name = object['__python_module__']
-	args = object['args']
-	state = object['state']
+	args = map(doh, object['args'])
+	state = dict((doh(key), doh(value)) for key, value in object['state'].items())
 	mod = _find_module_by_name(mod_name)
 	cls = getattr(mod, class_name)
 	ob = cls.__new__(cls, *args)