@@ -151,69 +151,94 @@ Basic Usage
151151 sort_keys=False, **kw)
152152
153153 Serialize *obj * as a JSON formatted stream to *fp * (a ``.write() ``-supporting
154- :term: `file-like object `) using this :ref: `conversion table
154+ :term: `file-like object `) using this :ref: `Python-to-JSON conversion table
155155 <py-to-json-table>`.
156156
157- If *skipkeys * is true (default: ``False ``), then dict keys that are not
158- of a basic type (:class: `str `, :class: `int `, :class: `float `, :class: `bool `,
159- ``None ``) will be skipped instead of raising a :exc: `TypeError `.
160-
161- The :mod: `json ` module always produces :class: `str ` objects, not
162- :class: `bytes ` objects. Therefore, ``fp.write() `` must support :class: `str `
163- input.
164-
165- If *ensure_ascii * is true (the default), the output is guaranteed to
166- have all incoming non-ASCII characters escaped. If *ensure_ascii * is
167- false, these characters will be output as-is.
168-
169- If *check_circular * is false (default: ``True ``), then the circular
170- reference check for container types will be skipped and a circular reference
171- will result in a :exc: `RecursionError ` (or worse).
157+ .. note ::
172158
173- If *allow_nan * is false (default: ``True ``), then it will be a
174- :exc: `ValueError ` to serialize out of range :class: `float ` values (``nan ``,
175- ``inf ``, ``-inf ``) in strict compliance of the JSON specification.
176- If *allow_nan * is true, their JavaScript equivalents (``NaN ``,
177- ``Infinity ``, ``-Infinity ``) will be used.
159+ Unlike :mod: `pickle ` and :mod: `marshal `, JSON is not a framed protocol,
160+ so trying to serialize multiple objects with repeated calls to
161+ :func: `dump ` using the same *fp * will result in an invalid JSON file.
178162
179- If *indent * is a non-negative integer or string, then JSON array elements and
180- object members will be pretty-printed with that indent level. An indent level
181- of 0, negative, or ``"" `` will only insert newlines. ``None `` (the default)
182- selects the most compact representation. Using a positive integer indent
183- indents that many spaces per level. If *indent * is a string (such as ``"\t" ``),
184- that string is used to indent each level.
163+ :param object obj:
164+ The Python object to be serialized.
165+
166+ :param fp:
167+ The file-like object *obj * will be serialized to.
168+ The :mod: `!json ` module always produces :class: `str ` objects,
169+ not :class: `bytes ` objects,
170+ therefore ``fp.write() `` must support :class: `str ` input.
171+ :type fp: :term: `file-like object `
172+
173+ :param bool skipkeys:
174+ If ``True ``, keys that are not of a basic type
175+ (:class: `str `, :class: `int `, :class: `float `, :class: `bool `, ``None ``)
176+ will be skipped instead of raising a :exc: `TypeError `.
177+ Default ``False ``.
178+
179+ :param bool ensure_ascii:
180+ If ``True `` (the default), the output is guaranteed to
181+ have all incoming non-ASCII characters escaped.
182+ If ``False ``, these characters will be outputted as-is.
183+
184+ :param bool check_circular:
185+ If ``False ``, the circular reference check for container types is skipped
186+ and a circular reference will result in a :exc: `RecursionError ` (or worse).
187+ Default ``True ``.
188+
189+ :param bool allow_nan:
190+ If ``False ``, serialization of out-of-range :class: `float ` values
191+ (``nan ``, ``inf ``, ``-inf ``) will result in a :exc: `ValueError `,
192+ in strict compliance with the JSON specification.
193+ If ``True `` (the default), their JavaScript equivalents
194+ (``NaN ``, ``Infinity ``, ``-Infinity ``) are used.
195+
196+ :param cls:
197+ If set, a custom JSON encoder with the
198+ :meth: `~JSONEncoder.default ` method overridden,
199+ for serializing into custom datatypes.
200+ If ``None `` (the default), :class: `!JSONEncoder ` is used.
201+ :type cls: a :class: `JSONEncoder ` subclass
202+
203+ :param indent:
204+ If a positive integer or string, JSON array elements and
205+ object members will be pretty-printed with that indent level.
206+ A positive integer indents that many spaces per level;
207+ a string (such as ``"\t" ``) is used to indent each level.
208+ If zero, negative, or ``"" `` (the empty string),
209+ only newlines are inserted.
210+ If ``None `` (the default), the most compact representation is used.
211+ :type indent: int | str | None
212+
213+ :param separators:
214+ A two-tuple: ``(item_separator, key_separator) ``.
215+ If ``None `` (the default), *separators * defaults to
216+ ``(', ', ': ') `` if *indent * is ``None ``,
217+ and ``(',', ': ') `` otherwise.
218+ For the most compact JSON,
219+ specify ``(',', ':') `` to eliminate whitespace.
220+ :type separators: tuple | None
221+
222+ :param default:
223+ A function that is called for objects that can't otherwise be serialized.
224+ It should return a JSON encodable version of the object
225+ or raise a :exc: `TypeError `.
226+ If ``None `` (the default), :exc: `!TypeError ` is raised.
227+ :type default: :term: `callable ` | None
228+
229+ :param bool sort_keys:
230+ If ``True ``, dictionaries will be outputted sorted by key.
231+ Default ``False ``.
185232
186233 .. versionchanged :: 3.2
187234 Allow strings for *indent * in addition to integers.
188235
189- If specified, *separators * should be an ``(item_separator, key_separator) ``
190- tuple. The default is ``(', ', ': ') `` if *indent * is ``None `` and
191- ``(',', ': ') `` otherwise. To get the most compact JSON representation,
192- you should specify ``(',', ':') `` to eliminate whitespace.
193-
194236 .. versionchanged :: 3.4
195237 Use ``(',', ': ') `` as default if *indent * is not ``None ``.
196238
197- If specified, *default * should be a function that gets called for objects that
198- can't otherwise be serialized. It should return a JSON encodable version of
199- the object or raise a :exc: `TypeError `. If not specified, :exc: `TypeError `
200- is raised.
201-
202- If *sort_keys * is true (default: ``False ``), then the output of
203- dictionaries will be sorted by key.
204-
205- To use a custom :class: `JSONEncoder ` subclass (e.g. one that overrides the
206- :meth: `~JSONEncoder.default ` method to serialize additional types), specify it with the
207- *cls * kwarg; otherwise :class: `JSONEncoder ` is used.
208-
209239 .. versionchanged :: 3.6
210240 All optional parameters are now :ref: `keyword-only <keyword-only_parameter >`.
211241
212- .. note ::
213-
214- Unlike :mod: `pickle ` and :mod: `marshal `, JSON is not a framed protocol,
215- so trying to serialize multiple objects with repeated calls to
216- :func: `dump ` using the same *fp * will result in an invalid JSON file.
217242
218243.. function :: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
219244 check_circular=True, allow_nan=True, cls=None, \
0 commit comments