In a project I have to output json file for some sort of entities. so this question is somehow a simplified version of my issue. Imagine I got these classes which I am gonna need their dict of properties and attributes as json output:
from random import randint
class A:
def __init__(self, a, b):
self.a = a
self.b = b
def out_dict(self):
return self.__dict__
class B:
def __init__(self, B_a, list_of_A):
self.b_arg = B_a
self.list_of_A = list_of_A
def out_dict(self):
return self.__dict__
class C:
def __init__(self, C_a, list_of_B):
self.c_arg = C_a
self.list_of_B = list_of_B
def out_dict(self):
return self.__dict__
list_of_A = [A(randint(0,100), randint(0,100)) for _ in range(5)]
list_of_B = [B(5, list_of_A) for _ in range(3)]
c = C(10, list_of_B)
print(c.out_dict())
the output of above code is:
{'c_arg': 10,
'list_of_B': [<__main__.B at 0x207613625c0>,
<__main__.B at 0x2076110bdd8>,
As you see, the objects are nested and the output does not mention all the attributes of classes in details. In the other hand, I need some output like this:
{"C":
{"list_of_B":[
"B0": {
{"list_of_A":
{["0":{"a":10, "b":21}, "2":{"a":1, "b":3},
"1":{"a":10, "b":21}, "2":{"a":54, "b":12},
"2":{"a":10, "b":21}, "2":{"a":12, "b":32},
]}
"b_arg": 27
}
},
"B1": {
{"list_of_A":
{["0":{"a":10, "b":21}, "2":{"a":1, "b":3},
"1":{"a":10, "b":21}, "2":{"a":54, "b":12},
"2":{"a":10, "b":21}, "2":{"a":12, "b":32},
]}
"b_arg": 27
}
},
]
"c_arg": 95
}
}
How can I get retrieve details in such circumstances.?
I need them to be outputted as json.
Thanks