summaryrefslogtreecommitdiff
path: root/docs/narr
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2012-04-18 00:50:10 -0500
committerMichael Merickel <michael@merickel.org>2012-04-18 00:50:10 -0500
commit677216d2c4ddc5f0df857b8f9e8fa6ccfd5fd55a (patch)
tree22f5fa97aeaf3583d07bc596159aee670343857d /docs/narr
parentc9ec6bd5130642095d77e66e672734296c6a513e (diff)
downloadpyramid-677216d2c4ddc5f0df857b8f9e8fa6ccfd5fd55a.tar.gz
pyramid-677216d2c4ddc5f0df857b8f9e8fa6ccfd5fd55a.tar.bz2
pyramid-677216d2c4ddc5f0df857b8f9e8fa6ccfd5fd55a.zip
reverted back to using a component registry during json encoding
Diffstat (limited to 'docs/narr')
-rw-r--r--docs/narr/renderers.rst32
1 files changed, 14 insertions, 18 deletions
diff --git a/docs/narr/renderers.rst b/docs/narr/renderers.rst
index 02063a112..c36caeb87 100644
--- a/docs/narr/renderers.rst
+++ b/docs/narr/renderers.rst
@@ -182,10 +182,10 @@ using the API of the ``request.response`` attribute. See
JSON Renderer
~~~~~~~~~~~~~
-The ``json`` renderer renders view callable results to :term:`JSON`. It
-passes the return value through the ``json.dumps`` standard library function,
-and wraps the result in a response object. It also sets the response
-content-type to ``application/json``.
+The ``json`` renderer renders view callable results to :term:`JSON`. By
+default, it passes the return value through the ``json.dumps`` standard
+library function, and wraps the result in a response object. It also sets
+the response content-type to ``application/json``.
Here's an example of a view that returns a dictionary. Since the ``json``
renderer is specified in the configuration for this view, the view will
@@ -209,11 +209,11 @@ 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 ``json.dumps``.
+values serializable by the configured serializer (by default ``json.dumps``).
.. note::
- Extra arguments can be passed to ``json.dumps`` by overriding the default
+ Extra arguments can be passed to the serializer by overriding the default
``json`` renderer. See :class:`pyramid.renderers.JSON` and
:ref:`adding_and_overriding_renderers` for more information.
@@ -240,8 +240,8 @@ 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).
+natively JSON-serializable (such as ints, lists, dictionaries, strings, and
+so forth).
.. code-block:: python
:linenos:
@@ -267,22 +267,18 @@ possible (or at least not reasonable) to add a custom ``__json__`` method to
to their classes in order to influence serialization. If the object passed
to the renderer is not a serializable type, and has no ``__json__`` method,
usually a :exc:`TypeError` will be raised during serialization. You can
-change this behavior by creating a JSON renderer with a "default" function
-which tries to "sniff" at the object, and returns a valid serialization (a
-string) or raises a TypeError if it can't determine what to do with the
-object. A short example follows:
+change this behavior by creating a custom JSON renderer and adding adapters
+to handle custom types. The renderer will attempt to adapt non-serializable
+objects using the registered adapters. It will raise a :exc:`TypeError` if it
+can't determine what to do with the object. A short example follows:
.. code-block:: python
:linenos:
from pyramid.renderers import JSON
- def default(obj):
- if isinstance(obj, datetime.datetime):
- return obj.isoformat()
- raise TypeError('%r is not serializable % (obj,))
-
- json_renderer = JSON(default=default)
+ json_renderer = JSON()
+ json_renderer.add_adapter(datetime.datetime, lambda x: x.isoformat())
# then during configuration ....
config = Configurator()