From 65914193587e9fba07d12908b28f0a974a1572b9 Mon Sep 17 00:00:00 2001 From: shakefu Date: Wed, 17 Apr 2013 13:58:18 -0700 Subject: Implement for_json kwarg; pure Python version; add tests; update documentation. --- index.rst | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'index.rst') diff --git a/index.rst b/index.rst index 4dd8a58..c9dd246 100644 --- a/index.rst +++ b/index.rst @@ -245,6 +245,13 @@ Basic Usage strings, to avoid comparison of heterogeneously typed objects (since this does not work in Python 3.3+) + If *for_json* is true (not the default), objects with a ``for_json()`` + method will use the return value of that method for encoding as JSON instead + of the object. + + .. versionchanged:: 3.2.0 + *for_json* is new in 3.2.0. + .. note:: JSON is not a framed protocol so unlike :mod:`pickle` or :mod:`marshal` it @@ -252,7 +259,7 @@ Basic Usage container protocol to delimit them. -.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, use_decimal[, namedtuple_as_object[, tuple_as_array[, bigint_as_string[, sort_keys[, item_sort_key[, **kw]]]]]]]]]]]]]]]]) +.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, use_decimal[, namedtuple_as_object[, tuple_as_array[, bigint_as_string[, sort_keys[, item_sort_key[, for_json[, **kw]]]]]]]]]]]]]]]]]) Serialize *obj* to a JSON formatted :class:`str`. @@ -456,7 +463,7 @@ Encoders and decoders :exc:`JSONDecodeError` will be raised if the given JSON document is not valid. -.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default[, use_decimal[, namedtuple_as_object[, tuple_as_array[, bigint_as_string[, item_sort_key]]]]]]]]]]]]]) +.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default[, use_decimal[, namedtuple_as_object[, tuple_as_array[, bigint_as_string[, item_sort_key[, for_json]]]]]]]]]]]]]]) Extensible JSON encoder for Python data structures. @@ -587,6 +594,12 @@ Encoders and decoders .. versionchanged:: 2.4.0 *bigint_as_string* is new in 2.4.0. + If *for_json* is true (default: ``False``), objects with a ``for_json()`` + method will use the return value of that method for encoding as JSON instead + of the object. + + .. versionchanged:: 3.2.0 + *for_json* is new in 3.2.0. .. method:: default(o) -- cgit v1.2.1