Using Colander to go from Python object to JSON

267 views
Skip to first unread message

Alex Kessinger

unread,
Nov 28, 2016, 4:02:03 PM11/28/16
to pylons-devel
Hi, I have a question about the appropriate use of Colander to go from a python object to JSON

Let's say I have a django model called Person, and a schema called PersonSchema.

Would this be an appropriate way to take a person model to json?

person = Person.objects.get(pk=1)
appstruct
= person.to_dict()
cstruct
= PersonSchema().serialize(appstruct)
response_body
= json.dumps(cstruct)

Is this about right? Should I instead just be going from dict to json?


Michael Merickel

unread,
Nov 28, 2016, 4:44:25 PM11/28/16
to pylons-devel
Colander is not suitable for serializing to json - its cstruct format is always strings. In my experience the only use of colander's serialization is for use with deform - however it's great for deserializing json to an appstruct. It's been a common complaint over the years but nothing much has been done to sort it out due to bw-compatibility issues.

If I were looking to serialize data to json and validate it, I'd recommend instead converting it to a json-compatible cstruct first, and then validating it (via deserialize) and skip serialize altogether. Alternatively you might look at the marshmallow library instead of colander which probably does a better job of coercing types while serializing.

appstruct = person.to_dict()
if validate(appstruct, PersonSchema()):
    return json.dumps(appstruct)

- Michael


--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel+unsubscribe@googlegroups.com.
To post to this group, send email to pylons...@googlegroups.com.
Visit this group at https://groups.google.com/group/pylons-devel.
For more options, visit https://groups.google.com/d/optout.

Alex Kessinger

unread,
Nov 29, 2016, 12:29:03 PM11/29/16
to pylons-devel
Thanks


On Monday, November 28, 2016 at 2:44:25 PM UTC-7, Michael Merickel wrote:
Colander is not suitable for serializing to json - its cstruct format is always strings. In my experience the only use of colander's serialization is for use with deform - however it's great for deserializing json to an appstruct. It's been a common complaint over the years but nothing much has been done to sort it out due to bw-compatibility issues.

If I were looking to serialize data to json and validate it, I'd recommend instead converting it to a json-compatible cstruct first, and then validating it (via deserialize) and skip serialize altogether. Alternatively you might look at the marshmallow library instead of colander which probably does a better job of coercing types while serializing.

appstruct = person.to_dict()
if validate(appstruct, PersonSchema()):
    return json.dumps(appstruct)

- Michael

On Mon, Nov 28, 2016 at 3:00 PM, Alex Kessinger <void...@gmail.com> wrote:
Hi, I have a question about the appropriate use of Colander to go from a python object to JSON

Let's say I have a django model called Person, and a schema called PersonSchema.

Would this be an appropriate way to take a person model to json?

person = Person.objects.get(pk=1)
appstruct
= person.to_dict()
cstruct
= PersonSchema().serialize(appstruct)
response_body
= json.dumps(cstruct)

Is this about right? Should I instead just be going from dict to json?


--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages