WorldMaker.netBlog2009December8 › Simple App Engine JSON Serialization Snippet

Simple App Engine JSON Serialization Snippet

I wanted a simple way to easily output JSON for a couple of AppEngine models. A brief search didn’t turn up a snippet that looked like what I was looking for, so I wrote this simple module that I call gaejson.py:

# Simple GAE Model JSON Serialization
# Copyright 2009 Max Battcher. Licensed for use under the Ms-PL.
from django.utils import simplejson as json
from google.appengine.ext import db

class GaeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, db.Model):
            return dict([(name, getattr(obj, name)) for name
                in obj.properties().keys()])
        return super(self, GaeEncoder).default(obj)

# vim: ai et ts=4 sts=4 sw=4

Usage is simple:

from gaejson import GaeEncoder, json
from mymodels import TestModel

test = TestModel.get(... some db key ...)
json.dumps(test, cls=GaeEncoder)
3 months ago

Posted on December 8, 2009 @00:24. Last Updated on December 8, 2009 @00:24.

Colophon Copyright © 1999-2009 Max Battcher / WorldMaker. Some Rights Reserved. With our thoughts, we make our worlds.