diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2013-05-07 11:24:57 -0400 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2013-05-07 11:24:57 -0400 |
commit | a7b22da4391facc843f61e585f83734226ddc441 (patch) | |
tree | 95780a34d1d149c7323270a8f03ae547459d76c6 /docs/source/jsonify.rst | |
parent | a53d9cdf255267321f23f5c2372f7d1500234739 (diff) | |
parent | e985be86ae89eccfe3c344196112b31a265ab741 (diff) | |
download | pecan-a072e5e7409e61e27f09da93c3f0f9ede53277f0.tar.gz |
Merge remote-tracking branch 'origin/next'0.2.4
Diffstat (limited to 'docs/source/jsonify.rst')
-rw-r--r-- | docs/source/jsonify.rst | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/docs/source/jsonify.rst b/docs/source/jsonify.rst index 73a1144..ac06c30 100644 --- a/docs/source/jsonify.rst +++ b/docs/source/jsonify.rst @@ -3,17 +3,18 @@ JSON Serialization ================== + Pecan includes a simple, easy-to-use system for generating and serving -``JSON``. To get started, create a file in your project called +JSON. To get started, create a file in your project called ``json.py`` and import it in your project's ``app.py``. Your ``json`` module will contain a series of rules for generating -``JSON`` from objects you return in your controller, utilizing +JSON from objects you return in your controller, utilizing "generic" function support from the `simplegeneric <http://pypi.python.org/pypi/simplegeneric>`_ library. Let's say that we have a controller in our Pecan application which -we want to use to return ``JSON`` output for a ``User`` object:: +we want to use to return JSON output for a :class:`User` object:: from myproject.lib import get_current_user @@ -27,8 +28,8 @@ we want to use to return ``JSON`` output for a ``User`` object:: return get_current_user() In order for this controller to function, Pecan will need to know how to -convert the ``User`` object into a ``JSON``-friendly data structure. One -way to tell Pecan how to convert an object into ``JSON`` is to define a +convert the :class:`User` object into data types compatible with JSON. One +way to tell Pecan how to convert an object into JSON is to define a rule in your ``json.py``:: from pecan.jsonify import jsonify @@ -42,14 +43,15 @@ rule in your ``json.py``:: birthday = user.birthday.isoformat() ) -In this example, when an instance of the ``model.User`` class is -returned from a controller which is configured to return ``JSON``, the -``jsonify_user`` rule will be called to generate that ``JSON``. Note -that the rule does not generate a ``JSON`` string, but rather generates -a Python dictionary which contains only ``JSON`` friendly data types. +In this example, when an instance of the :class:`model.User` class is +returned from a controller which is configured to return JSON, the +:func:`jsonify_user` rule will be called to convert the object to +JSON-compatible data. Note that the rule does not generate a JSON +string, but rather generates a Python dictionary which contains only +JSON friendly data types. Alternatively, the rule can be specified on the object itself, by -specifying a ``__json__`` method on the object:: +specifying a :func:`__json__` method in the class:: class User(object): def __init__(self, name, email, birthday): @@ -64,7 +66,7 @@ specifying a ``__json__`` method on the object:: birthday = self.birthday.isoformat() ) -The benefit of using a ``json.py`` module is having all of your ``JSON`` +The benefit of using a ``json.py`` module is having all of your JSON rules defined in a central location, but some projects prefer the -simplicity of keeping the ``JSON`` rules attached directly to their +simplicity of keeping the JSON rules attached directly to their model objects. |