erichiggins / gaek

A collection of useful tools for Google App Engine.
MIT License
16 stars 6 forks source link

GAEK: Google App Engine Kit

Build Status

A collection of useful tools for Python apps running on Google App Engine.

NDB JSON module

Usage:

from gaek import ndb_json

# Serialize NDB Model(s) to a JSON string.
json_str = ndb_json.dumps(models)

# Parse a JSON string into a Python dictionary.
ndb_json.loads(json_str)

When the encoder meets a property of the ndb.Key type, there are three encoding options available:

Please refer to NDB Key Class documentation for details.

For example, for the following data models:

    class Master(ndb.Model):
      name = ndb.StringProperty()
    class Details(ndb.Model):
      master = ndb.KeyProperty()
      description = ndb.StringProperty()

and following records:

    master = Master(id=123456L, name='Europe')
    details = Details(
      master=ndb.Key(Master, 123456L), 
      description='List of European customers'
     )

The calls

    json_str = ndb_json.dumps(details)
    json_str = ndb_json.dumps(details, ndb_keys_as_entities=True)

will return

{"master": {"name": "Europe"}, "description": "List of European customers"}

The call

    json_str = ndb_json.dumps(details, ndb_keys_as_pairs=True)

will return

{"master": [["Master", 123456]], "description": "List of European customers"}

The call

    json_str = ndb_json.dumps(details, ndb_keys_as_urlsafe=True)

will return

{"master": "agFfcg4LEgZNYXN0ZXIYwMQHDA", "description": "List of European customers"}

Feature parity with the Python json module functions.

Environment module