summaryrefslogtreecommitdiff
path: root/docs/narr/renderers.rst
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2012-04-16 23:19:14 -0500
committerMichael Merickel <michael@merickel.org>2012-04-16 23:19:14 -0500
commit1f0d9d2193bb9557d4475885776b5679c8dbfa23 (patch)
tree7934c995bb6f05187c8396679484d9f964021f32 /docs/narr/renderers.rst
parentdfaf54a7018971c08ed4a437dbec0ffb57d1ff8a (diff)
downloadpyramid-1f0d9d2193bb9557d4475885776b5679c8dbfa23.tar.gz
pyramid-1f0d9d2193bb9557d4475885776b5679c8dbfa23.tar.bz2
pyramid-1f0d9d2193bb9557d4475885776b5679c8dbfa23.zip
docs for json defaults
Diffstat (limited to 'docs/narr/renderers.rst')
-rw-r--r--docs/narr/renderers.rst29
1 files changed, 15 insertions, 14 deletions
diff --git a/docs/narr/renderers.rst b/docs/narr/renderers.rst
index 34bee3c7f..50349c409 100644
--- a/docs/narr/renderers.rst
+++ b/docs/narr/renderers.rst
@@ -223,9 +223,9 @@ You can configure a view to use the JSON renderer by naming ``json`` as the
:linenos:
config.add_view('myproject.views.hello_world',
- name='hello',
- context='myproject.resources.Hello',
- renderer='json')
+ name='hello',
+ context='myproject.resources.Hello',
+ renderer='json')
Views which use the JSON renderer can vary non-body response attributes by
using the api of the ``request.response`` attribute. See
@@ -260,20 +260,21 @@ strings, and so forth).
# the JSON value returned by ``objects`` will be:
# [{"x": 1}, {"x": 2}]
-.. note::
+If you don't own the objects being serialized, it's difficult to add a custom
+``__json__`` method to the object. In this case, a callback can be supplied
+to the renderer which is invoked when other options have failed.
- Honoring the ``__json__`` method of custom objects is a feature new in
- Pyramid 1.4.
+.. code-block:: python
+ :linenos:
+
+ def default(obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.isoformat()
+ raise TypeError
-.. warning::
+.. note::
- 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`.
+ Serializing custom objects is a feature new in Pyramid 1.4.
.. index::
pair: renderer; JSONP