I have a python object which contains a list of other python objects which in turn contain array of another object. Simple representation:
class FirstClass:
objects_list = [] #list of instances of the Second Class
def __init__(self, objects_list: list):
self.objects_list = objects_list
#-----------------------------------
class SecondClass:
another_objects_list = [] #list of the ThirdClass instances
def __init__(self, another_objects_list: list):
self.another_objects_list = another_objects_list
#------------------------------------
class ThirdClass:
v = ""
I need to serialize the FirstClass
and all its members, including the members of the SecondClass
and ThirdClass
classes. Unfortunately I have to do it with the built in json
module which doesn't serialize custom complex object by itself. I know I can serialize simple class with json.dumps(object.__dic__)
, but I have no idea how to get it working in my case.
I tried this but it didn't work:
json.dumps(object.__dict__, default=[lambda e: e.__dict__
for e in object.nested_objects], indent=4)