I have a Properties class :
from child_props import ChildProps
class ParentProps(object):
"""Contains all the attributes for CreateOrderRequest"""
def __init__(self):
self.__prop1 = None
self.__child_props = ChildProps()
@property
def prop1(self):
return self.__prop1
@prop1.setter
def prop1(self, value):
self.__prop1 = value
@property
def child_props(self):
return self.__child_props
@child_props.setter
def child_props(self, value):
self.__child_props = value
And Another class is :
class ChildProps(object):
"""Contains all the attributes for CreateOrderRequest"""
def __init__(self):
self.__child_prop1 = None
self.__child_prop2 = None
@property
def child_prop1(self):
return self.__child_prop1
@child_prop1.setter
def child_prop1(self, value):
self.__child_prop1 = value
@property
def child_prop2(self):
return self.__child_prop2
@child_prop2.setter
def child_prop2(self, value):
self.__child_prop2 = value
In main.py
parent_props = ParentProps()
parent_props.prop1 = "Mark"
child_props = ChildProps()
child_props.child_prop1 = 'foo'
child_props.child_prop2 = 'bar'
parent_props.child_props = child_props
How to serialize parent_props to json string like below :
{
"prop1" : "Mark",
"child_props" : {
"child_prop1" : "foo",
"child_prop2" : "bar"
}
}
PS : json.dumps can only serialize native python datatypes. pickle module only does object serialization to bytes.
Just like we have NewtonSoft in dotnet, jackson in java, what is the equivalent serializer in Python to serialize getter setter properties class object.
I have serached a lot in google but couldn't get much help. Any lead will be much appreciable. Thanks
json.dumpsthe dict.@propertyand the__init__and the JSON serialization, but SO isn't a good place to ask for library recommendations. If that's what you want, and you can't find it by searching, try the Community section on the python.org website (probably either the python-list mailing list or #python IRC channel).