This API Documentation is currently a catch-all. We’re going to merge the API docs into the hand created docs as we have time to integrate them.
JSON Helper functions. Most JSON code directly related to classes is implemented via the __json__() methods in model.py. These methods define methods of transforming a class into json for a few common types.
A JSON-based API(view) for your app. Most rules would look like:
@jsonify.when("isinstance(obj, YourClass)")
def jsonify_yourclass(obj):
return [obj.val1, obj.val2]
@jsonify can convert your objects to following types: lists, dicts, numbers and strings
Module author: Toshio Kuratomi <tkuratom@redhat.com>
Transform selectresults into lists.
The one special thing is that we bind the special json_props into each descendent. This allows us to specify a json_props on the toplevel query result and it will pass to all of its children.
Parameters: | obj – sqlalchemy Query object to jsonify |
---|---|
Returns : | list representation of the Query with each element in it given a json_props attributes |
Transform SQLAlchemy InstrumentedLists into json.
The one special thing is that we bind the special json_props into each descendent. This allows us to specify a json_props on the toplevel query result and it will pass to all of its children.
Parameters: | obj – One of the sqlalchemy list types to jsonify |
---|---|
Returns : | list of jsonified elements |
Transform SQLAlchemy ResultProxy into json.
The one special thing is that we bind the special json_props into each descendent. This allows us to specify a json_props on the toplevel query result and it will pass to all of its children.
Parameters: | obj – sqlalchemy ResultProxy to jsonify |
---|---|
Returns : | list of jsonified elements |
Transform a set into a list.
simplejson doesn’t handle sets natively so transform a set into a list.
Parameters: | obj – set to jsonify |
---|---|
Returns : | list representation of the set |
Base class for SQLAlchemy mapped objects.
This base class makes sure we have a __json__() method on each SQLAlchemy mapped object that knows how to:
Transform any SA mapped class into json.
This method takes an SA mapped class and turns the “normal” python attributes into json. The properties (from properties in the mapper) are also included if they have an entry in json_props. You make use of this by setting json_props in the controller.
Example controller:
john = model.Person.get_by(name='John')
# Person has a property, addresses, linking it to an Address class.
# Address has a property, phone_nums, linking it to a Phone class.
john.json_props = {'Person': ['addresses'],
'Address': ['phone_nums']}
return dict(person=john)
json_props is a dict that maps class names to lists of properties you want to output. This allows you to selectively pick properties you are interested in for one class but not another. You are responsible for avoiding loops. ie: don’t do this:
john.json_props = {'Person': ['addresses'], 'Address': ['people']}