6

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

4
  • 2
    The easy way to do it is to write a function that generates that dict, and then json.dumps the dict. Commented Mar 26, 2018 at 5:49
  • There are a bunch of third-party libraries to assist in serializing to JSON, or even in generating classes declaratively in a way that builds the @property and 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). Commented Mar 26, 2018 at 5:51
  • Thanks abarnet for reply. One can write the separate serializer to do json serialization but like in other programming language, we have a popular serializer to do that task without writing separate serializer for each properties class. I am looking for that serializer. I have already checked python.org website and IRC channel too but couldn't get much help, thats why posted here. I hope u got my point. Commented Mar 26, 2018 at 5:56
  • Sorry, but if you're asking for library recommendations, SO can't help you there. See the help center pages for more information. Commented Mar 26, 2018 at 6:04

1 Answer 1

1

Check this:

def serializable_attrs(self):
    return (dict(
        (i.replace(self.__class__.__name__, '').lstrip("_"), value)
        for i, value in self.__dict__.items()
    ))

It should return a dictionary with properties of your class.

I replaced class name because properties in __dict__ will look like: _ChildProps___child_prop1.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.