I know there are a lot of question on JSON serialization, but I'm still not grasping this it seems.
Given the following class:
import json
class BaseMongoObject(object):
def __init__(self):
pass
def jsonify(self):
return json.dumps(self, default=lambda o: o.__dict)
And the following derived class:
from assetfacts import AssetFacts
from bmo import BaseMongoObject
class Asset(BaseMongoObject):
def __init__(self):
BaseMongoObject.__init__(self)
self.facts = AssetFacts()
self.serial = None
Trying to call asset.jsonify()
by using the following piece of test code:
from asset import Asset
def test_me():
a = Asset()
a.serial = '123asdf'
print a.jsonify()
if __name__ == '__main__':
test_me()
Produces the following:
Traceback (most recent call last):
File "../bin/test.py", line 17, in <module>
test_me()
File "../bin/test.py", line 13, in test_me
print a.jsonify()
File "/Users/vlazarenko/MP/ac/lib/bmo.py", line 8, in jsonify
return json.dumps(self, default=lambda o: o.__dict)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/Users/vlazarenko/MP/ac/lib/bmo.py", line 8, in <lambda>
return json.dumps(self, default=lambda o: o.__dict)
AttributeError: 'Asset' object has no attribute '_BaseMongoObject__dict'
Where am I going braindead about this? Ideally I wouldn't wanna be bothered with amount of levels of inheritance, just serialize all the way from the top.