views:

485

answers:

1

I have a large hierarchical dataset in the App Engine Datastore. The hierarchy is preserved by storing the data in Entity groups, so that I can pull a whole tree by simply knowing the top element key like so:

query = db.Query().ancestor(db.get(key))

The question: How do I now output this data as JSON and preserve the hierarchy?

Google has a utility class called GqlEncoder that add support for datastore query results to simplejson, but it basically flattens the data, destroying the hierarchy.

Any suggestions?

+1  A: 

I imagine you're referring to this code and the "flattening" you mention is done by lines 51-52:

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

while the rest of the code is fine for your purpose. So, how would you like to represent a GQL query, since you don't what a JS array (Python list) of the objects it contains? It's not clear, besides the entity group (which you're recovering entirely), what gives it hierarchy; is it an issue of "parent"?

Anyway, once that's clarified, copying and editing that file into your own code seems best (it's not designed to let you override just that one tidbit).

Alex Martelli
It's an issue of 'parent' and allowing that as the hierarchy.I could modify the existing library, but I figured I'd check here first to see if there was a library that already supported it.
Christian
@Christian, good thinking, but I don't know of one. I suggest you extract that `list(obj)` use into a separate method so the base class can have unchanged functionality and a subclass can override the method and perform the extra processing you need (I assume you want a nested list-of-lists?) while reusing 99% of the base class's functionality -- such a patch might be accepted into the open source parts of app engine, and then you wouldn't have to maintain it yourself in the future.
Alex Martelli