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 <voidfi...@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+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-devel@googlegroups.com.
> Visit this group at https://groups.google.com/group/pylons-devel.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/pylons-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to