RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: The problem: JSON renderer for custom types

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now you might feel like we've done everything there is to do with this json renderer, the built in one,
0:06 but, there is a really important question we have to address before we move on. So, using it is straightforward as we've seen,
0:12 we just set the renderer to json, and don't have to do anything, pyramid already knows about that, that's cool,
0:18 and then we just return some object that is serializable as an object for the json renderer, so basically if you can use the json module in Python,
0:28 to serialize this, then it will work fine here, okay. Now, what happens if that cars object
0:35 that we were returning from that method and letting the renderer serialize what if it contains some kind of custom type, right,
0:42 like an entity you might get back from the database or something like that, well, maybe everything will be okay, however, it's not okay;
0:50 here is what happens if you try to return a type that is some kind of built in, so you can see right here in the error, we're returning a car instance,
0:58 car object at ox106 etc, it's some memory address and it says this thing is not json serializable.
1:05 However, it is very common to have these types of objects, right, if we're using sqlalchemy to get something from the database,
1:12 mongo engine to get something from mongo db all sorts of reasons we might have our own custom objects even things as simple as datetimes,
1:20 like datetimes ar not json serializable, what do you there, right, you better hope you don't have a thing that has a date, that's really common.
1:26 So what we're going to talk about now is how do we configure the json, the built in json renderer to make this error go away
1:34 and serialize everything perfectly.


Talk Python's Mastodon Michael Kennedy's Mastodon