Rien de spécial
Le blog de Régis

Serialize db.Blob in JSON

For a couple of days, I’ve been playing with google-app-engine. In order to build a JSON-based RESTful service, Google suggests a utility method to transform an object into JSON.

Unfortunately, this does not handle db/Blob entries, and fails with

<br /> UnicodeDecodeError: 'utf8' codec can't decode byte 0x89 in position 0: invalid start byte<br />

(I have a png image in my blob).

After struggling with it, I eventually found the fix (also posted on stackoverflow)

class GqlEncoder(json.JSONEncoder):
«  » »Extends JSONEncoder to add support for GQL results and properties.

Adds support to simplejson JSONEncoders for GQL results and properties by
overriding JSONEncoder’s default method.
«  » »
def default(self, obj):
if hasattr(obj, &lsquo;\_\_json\_\_’):
return getattr(obj, &lsquo;\_\_json\_\_’)()

if isinstance(obj, db.GqlQuery):
return list(obj)

elif isinstance(obj, db.Model):
properties = obj.properties().items()
output = {}
for field, value in properties:
data = getattr(obj, field)
if isinstance(data, str):
\# db.Blob inherits from str
data = data.encode(&lsquo;string-escape’)
output[field] = data
output[&lsquo;id’] = obj.key().id()
return output