summaryrefslogtreecommitdiff
path: root/docs/narr/renderers.rst
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2012-03-29 04:21:30 -0400
committerChris McDonough <chrism@plope.com>2012-03-29 04:21:30 -0400
commitde797c4cefb03f16cfe3505c85d94c0af24eb066 (patch)
tree226eca892311341011e5cdcaf5e4861e65e9d6ae /docs/narr/renderers.rst
parentba60524b56a639ecad42f85b63af2120d9d96cdc (diff)
downloadpyramid-de797c4cefb03f16cfe3505c85d94c0af24eb066.tar.gz
pyramid-de797c4cefb03f16cfe3505c85d94c0af24eb066.tar.bz2
pyramid-de797c4cefb03f16cfe3505c85d94c0af24eb066.zip
- Coverage and docs updates for custom JSON class.
- Fork point: master now represents a future 1.4 release.
Diffstat (limited to 'docs/narr/renderers.rst')
-rw-r--r--docs/narr/renderers.rst68
1 files changed, 56 insertions, 12 deletions
diff --git a/docs/narr/renderers.rst b/docs/narr/renderers.rst
index 52e97d091..34bee3c7f 100644
--- a/docs/narr/renderers.rst
+++ b/docs/narr/renderers.rst
@@ -207,17 +207,13 @@ representing the JSON serialization of the return value:
'{"content": "Hello!"}'
The return value needn't be a dictionary, but the return value must contain
-values serializable by :func:`json.dumps`. Extra arguments can be passed
-to :func:`json.dumps` by overriding the default renderer. See
-:class:`pyramid.renderers.JSON` and
-:ref:`_adding_and_overriding_renderers` for more information.
-
-Custom objects can be easily serialized by defining a :func:`__json__` method
-on the object. This method should return values serializable by
-:func:`json_dumps`. By defining this method and using a :term:`JSON`
-renderer the :class:`pyramid.renderers.ObjectJSONEncoder` class will be used
-for encoding your object. If you later define your own custom encoder it will
-override :class:`pyramid.renderers.ObjectJSONEncoder`.
+values serializable by ``json.dumps``.
+
+.. note::
+
+ Extra arguments can be passed to ``json.dumps`` by overriding the default
+ ``json`` renderer. See :class:`pyramid.renderers.JSON` and
+ :ref:`adding_and_overriding_renderers` for more information.
You can configure a view to use the JSON renderer by naming ``json`` as the
``renderer`` argument of a view configuration, e.g. by using
@@ -235,13 +231,57 @@ Views which use the JSON renderer can vary non-body response attributes by
using the api of the ``request.response`` attribute. See
:ref:`request_response_attr`.
+.. _json_serializing_custom_objects:
+
+Serializing Custom Objects
+++++++++++++++++++++++++++
+
+Custom objects can be made easily JSON-serializable in Pyramid by defining a
+``__json__`` method on the object's class. This method should return values
+natively serializable by ``json.dumps`` (such as ints, lists, dictionaries,
+strings, and so forth).
+
+.. code-block:: python
+ :linenos:
+
+ from pyramid.view import view_config
+
+ class MyObject(object):
+ def __init__(self, x):
+ self.x = x
+
+ def __json__(self):
+ return {'x':self.x}
+
+ @view_config(renderer='json')
+ def objects(request):
+ return [MyObject(1), MyObject(2)]
+
+ # the JSON value returned by ``objects`` will be:
+ # [{"x": 1}, {"x": 2}]
+
+.. note::
+
+ Honoring the ``__json__`` method of custom objects is a feature new in
+ Pyramid 1.4.
+
+.. warning::
+
+ The machinery which performs the ``__json__`` method-calling magic is in
+ the :class:`pyramid.renderers.ObjectJSONEncoder` class. This class will
+ be used for encoding any non-basic Python object when you use the default
+ ```json`` or ``jsonp`` renderers. But if you later define your own custom
+ JSON renderer and pass it a "cls" argument signifying a different encoder,
+ the encoder you pass will override Pyramid's use of
+ :class:`pyramid.renderers.ObjectJSONEncoder`.
+
.. index::
pair: renderer; JSONP
.. _jsonp_renderer:
JSONP Renderer
---------------
+~~~~~~~~~~~~~~
.. note:: This feature is new in Pyramid 1.1.
@@ -306,6 +346,10 @@ The string ``callback=?`` above in the the ``url`` param to the JQuery
a JSONP request; the ``callback`` parameter will be automatically filled
in for you and used.
+The same custom-object serialization scheme defined used for a "normal" JSON
+renderer in :ref:`json_serializing_custom_objects` can be used when passing
+values to a JSONP renderer too.
+
.. index::
pair: renderer; chameleon