Menu

JSON serialization of Google App Engine models

For some AJAX requests you’ll need an rpc urls returning a serialized objects. In principle its simple just use JSON serialization build in in Django or simplejson module itself. Here are sample view methods:

But there’s a catch. Django will only serialize properties no other fields (eg. computed fields or properties) will be serialized, and simplejson will complain that your Entity is not serializable.

In the first case if you don’t need anything else than data store properties, you’re ok. I wanted the get_absolute_url() to be serialized, it comes handy on client side and it’s very DRY if do not compute urls on client side. To do that I have overridden a default JSON serialization module, creating a json_serializer.py:

In the second case, all I wanted was name:key pairs to be serialized, this was simpler. I have provided a default argument with callable doing the serialization for my type, simple lambda expression did the trick: