hitrust / google-app-engine-samples

Automatically exported from code.google.com/p/google-app-engine-samples
0 stars 0 forks source link

Support for json.encode() on db.Blob #58

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Add a db.Blob in a model
2. Use json.encode()

What is the expected output? What do you see instead?

I expect a JSON serialization of the object.

Instead, an UnicodeDecodeError is raised. 

Original issue reported on code.google.com by regis.de...@gmail.com on 2 Feb 2012 at 1:31

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Here is my fix for 
http://code.google.com/p/google-app-engine-samples/source/browse/trunk/geochat/j
son.py

http://stackoverflow.com/a/9105898/94363

        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('string-escape')
                output[field] = data
            output['id'] = obj.key().id()
            return output

Original comment by regis.de...@gmail.com on 2 Feb 2012 at 1:33