1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import json import uuid
class MyClass(object):
def __init__(self, name): self.uid = uuid.uuid1() self.name = name
def info(self): return { 'uid': self.uid.hex, 'name': self.name, }
class MyJSONEncoder(json.JSONEncoder):
def default(self, o): if isinstance(o, MyClass): return o.info() return super(MyJSONEncoder, self).default(o)
|