Python: Serialize object to json - Stack Overflow
stackoverflow.com › questions › 43295958Apr 08, 2017 · import os import sys import json class ComplexEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, P): return json.dumps(obj.__dict__) # Let the base class default method raise the TypeError return json.JSONEncoder.default(self, obj) class P(object): def __init__(self): self.name = "Unknown" self.id = 1 self.lst = [] def showName(self): print(self.name) for item in self.lst: item.showName() def add(self, p): self.lst.append(p) def serialize(self): return json.dumps(self, cls ...