If it's a set type,then turn the set into list before serializing, or use a custom default
handler to do so:
1 def set_default(obj): 2 if isinstance(obj, set): 3 return list(obj) 4 raise TypeError 5 6 result = json.dumps(yourdata, default=set_default)
If you want a dict type,Maybe it's not actually a dictionary; it's another mapping type that looks like a dictionary. Use type()
to verify. Pass it to dict()
to get a real dictionary from it.
1 def __init__(self, element): 2 self.element = element 3 4 def execute(self): 5 if isinstance(self.element, dict): 6 self.__iterate() 7 else: 8 return 9 10 def __iterate(self): 11 for key in self.element: 12 if isinstance(self.element[key], <ClassName>): 13 self.element[key] = str(self.element[key]) 14 15 node = NormalizeData(self.element[key]) 16 node.execute()