I'm building in tornado (cyclone, actually), and RequestHandler.write is choking on some of my objects. How do I write a JSONencoder for these objects in tornado?
One complication: some of the objects are borrowed from external libraries, so I don't really have access to their constructors.
Apologies for not posting code -- I'm just not sure how to begin here.
Basically, the answer is that tornado doesn't support custom json formatting, so you have to use the json library. Here's the code I used in the end:
import json
class MongoEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, ObjectId):
return str(obj)
return json.JSONEncoder.default(self, obj)
print json.dumps(my_mong_obj, cls=MongoEncoder, indent=2)