GoogleAppEngine 模型的 JSON 序列化

我找了半天也没找到。我的项目没有使用 Django,有没有一种简单的方法来序列化 App Engine 模型(google.appengineering.ext.db)。模型)转换为 JSON,还是需要编写自己的序列化程序?

型号:

class Photo(db.Model):
filename = db.StringProperty()
title = db.StringProperty()
description = db.StringProperty(multiline=True)
date_taken = db.DateTimeProperty()
date_uploaded = db.DateTimeProperty(auto_now_add=True)
album = db.ReferenceProperty(Album, collection_name='photo')
35773 次浏览

Even if you are not using django as a framework, those libraries are still available for you to use.

from django.core import serializers
data = serializers.serialize("xml", Photo.objects.all())

You don't need to write your own "parser" (a parser would presumably turn JSON into a Python object), but you can still serialize your Python object yourself.

Using simplejson:

import simplejson as json
serialized = json.dumps({
'filename': self.filename,
'title': self.title,
'date_taken': date_taken.isoformat(),
# etc.
})

A simple recursive function can be used to convert an entity (and any referents) to a nested dictionary that can be passed to simplejson:

import datetime
import time


SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)


def to_dict(model):
output = {}


for key, prop in model.properties().iteritems():
value = getattr(model, key)


if value is None or isinstance(value, SIMPLE_TYPES):
output[key] = value
elif isinstance(value, datetime.date):
# Convert date/datetime to MILLISECONDS-since-epoch (JS "new Date()").
ms = time.mktime(value.utctimetuple()) * 1000
ms += getattr(value, 'microseconds', 0) / 1000
output[key] = int(ms)
elif isinstance(value, db.GeoPt):
output[key] = {'lat': value.lat, 'lon': value.lon}
elif isinstance(value, db.Model):
output[key] = to_dict(value)
else:
raise ValueError('cannot encode ' + repr(prop))


return output

For simple cases, I like the approach advocated here at the end of the article:

  # after obtaining a list of entities in some way, e.g.:
user = users.get_current_user().email().lower();
col = models.Entity.gql('WHERE user=:1',user).fetch(300, 0)


# ...you can make a json serialization of name/key pairs as follows:
json = simplejson.dumps(col, default=lambda o: {o.name :str(o.key())})

The article also contains, at the other end of the spectrum, a complex serializer class that enriches django's (and does require _meta -- not sure why you're getting errors about _meta missing, perhaps the bug described here) with the ability to serialize computed properties / methods. Most of the time you serialization needs lay somewhere in between, and for those an introspective approach such as @David Wilson's may be preferable.

If you use app-engine-patch it will automatically declare the _meta attribute for you, and then you can use django.core.serializers as you would normally do on django models (as in sledge's code).

App-engine-patch has some other cool features such has an hybrid authentication (django + google accounts), and the admin part of django works.

This is the simplest solution I found. It requires only 3 lines of codes.

Simply add a method to your model to return a dictionary:

class DictModel(db.Model):
def to_dict(self):
return dict([(p, unicode(getattr(self, p))) for p in self.properties()])

SimpleJSON now works properly:

class Photo(DictModel):
filename = db.StringProperty()
title = db.StringProperty()
description = db.StringProperty(multiline=True)
date_taken = db.DateTimeProperty()
date_uploaded = db.DateTimeProperty(auto_now_add=True)
album = db.ReferenceProperty(Album, collection_name='photo')


from django.utils import simplejson
from google.appengine.ext import webapp


class PhotoHandler(webapp.RequestHandler):
def get(self):
photos = Photo.all()
self.response.out.write(simplejson.dumps([p.to_dict() for p in photos]))

There's a method, "Model.properties()", defined for all Model classes. It returns the dict you seek.

from django.utils import simplejson
class Photo(db.Model):
# ...


my_photo = Photo(...)
simplejson.dumps(my_photo.properties())

See Model properties in the docs.

To serialize models, add a custom json encoder as in the following python:

import datetime
from google.appengine.api import users
from google.appengine.ext import db
from django.utils import simplejson


class jsonEncoder(simplejson.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()


elif isinstance(obj, db.Model):
return dict((p, getattr(obj, p))
for p in obj.properties())


elif isinstance(obj, users.User):
return obj.email()


else:
return simplejson.JSONEncoder.default(self, obj)




# use the encoder as:
simplejson.dumps(model, cls=jsonEncoder)

This will encode:

  • a date as as isoformat string (per this suggestion),
  • a model as a dict of its properties,
  • a user as his email.

To decode the date you can use this javascript:

function decodeJsonDate(s){
return new Date( s.slice(0,19).replace('T',' ') + ' GMT' );
} // Note that this function truncates milliseconds.

Note: Thanks to user pydave who edited this code to make it more readable. I had originally had used python's if/else expressions to express jsonEncoder in fewer lines as follows: (I've added some comments and used google.appengine.ext.db.to_dict, to make it clearer than the original.)

class jsonEncoder(simplejson.JSONEncoder):
def default(self, obj):
isa=lambda x: isinstance(obj, x) # isa(<type>)==True if obj is of type <type>
return obj.isoformat() if isa(datetime.datetime) else \
db.to_dict(obj) if isa(db.Model) else \
obj.email()     if isa(users.User) else \
simplejson.JSONEncoder.default(self, obj)

Mtgred's answer above worked wonderfully for me -- I slightly modified it so I could also get the key for the entry. Not as few lines of code, but it gives me the unique key:

class DictModel(db.Model):
def to_dict(self):
tempdict1 = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
tempdict2 = {'key':unicode(self.key())}
tempdict1.update(tempdict2)
return tempdict1

In the latest (1.5.2) release of the App Engine SDK, a to_dict() function that converts model instances to dictionaries was introduced in db.py. See the release notes.

There is no reference to this function in the documentation as of yet, but I have tried it myself and it works as expected.

I've extended the JSON Encoder class written by dpatru to support:

  • Query results properties (e.g. car.owner_set)
  • ReferenceProperty - recursively turn it into JSON
  • Filtering properties - only properties with a verbose_name will be encoded into JSON

    class DBModelJSONEncoder(json.JSONEncoder):
    """Encodes a db.Model into JSON"""
    
    
    def default(self, obj):
    if (isinstance(obj, db.Query)):
    # It's a reference query (holding several model instances)
    return [self.default(item) for item in obj]
    
    
    elif (isinstance(obj, db.Model)):
    # Only properties with a verbose name will be displayed in the JSON output
    properties = obj.properties()
    filtered_properties = filter(lambda p: properties[p].verbose_name != None, properties)
    
    
    # Turn each property of the DB model into a JSON-serializeable entity
    json_dict = dict([(
    p,
    getattr(obj, p)
    if (not isinstance(getattr(obj, p), db.Model))
    else
    self.default(getattr(obj, p)) # A referenced model property
    ) for p in filtered_properties])
    
    
    json_dict['id'] = obj.key().id() # Add the model instance's ID (optional - delete this if you do not use it)
    
    
    return json_dict
    
    
    else:
    # Use original JSON encoding
    return json.JSONEncoder.default(self, obj)
    

As mentioned by https://stackoverflow.com/users/806432/fredva, the to_dict works great. Here is my code i'm using.

foos = query.fetch(10)
prepJson = []


for f in foos:
prepJson.append(db.to_dict(f))


myJson = json.dumps(prepJson))

To serialize a Datastore Model instance you can't use json.dumps (haven't tested but Lorenzo pointed it out). Maybe in the future the following will work.

http://docs.python.org/2/library/json.html

import json
string = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
object = json.loads(self.request.body)

These APIs (google.appengine.ext.db) are no longer recommended. Apps that use these APIs can only run in the App Engine Python 2 runtime and will need to migrate to other APIs and services before migrating to the App Engine Python 3 runtime. To know more: click here