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