Serialize ``obj`` to a JSON formatted ``str``. If ``skipkeys`` is true then ``dict`` keys that are not basic types (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped instead of raising a ``TypeError``. If ``ensure_ascii`` is false, then the return value can contain
(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw)
| 182 | |
| 183 | |
| 184 | def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, |
| 185 | allow_nan=True, cls=None, indent=None, separators=None, |
| 186 | default=None, sort_keys=False, **kw): |
| 187 | """Serialize ``obj`` to a JSON formatted ``str``. |
| 188 | |
| 189 | If ``skipkeys`` is true then ``dict`` keys that are not basic types |
| 190 | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
| 191 | instead of raising a ``TypeError``. |
| 192 | |
| 193 | If ``ensure_ascii`` is false, then the return value can contain |
| 194 | non-ASCII and non-printable characters if they appear in strings |
| 195 | contained in ``obj``. Otherwise, all such characters are escaped in |
| 196 | JSON strings. |
| 197 | |
| 198 | If ``check_circular`` is false, then the circular reference check |
| 199 | for container types will be skipped and a circular reference will |
| 200 | result in an ``RecursionError`` (or worse). |
| 201 | |
| 202 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
| 203 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in |
| 204 | strict compliance of the JSON specification, instead of using the |
| 205 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 206 | |
| 207 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 208 | object members will be pretty-printed with that indent level. An indent |
| 209 | level of 0 will only insert newlines. ``None`` is the most compact |
| 210 | representation. |
| 211 | |
| 212 | If specified, ``separators`` should be an ``(item_separator, |
| 213 | key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is |
| 214 | ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON |
| 215 | representation, you should specify ``(',', ':')`` to eliminate |
| 216 | whitespace. |
| 217 | |
| 218 | ``default(obj)`` is a function that should return a serializable version |
| 219 | of obj or raise TypeError. The default simply raises TypeError. |
| 220 | |
| 221 | If *sort_keys* is true (default: ``False``), then the output of |
| 222 | dictionaries will be sorted by key. |
| 223 | |
| 224 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 225 | ``.default()`` method to serialize additional types), specify it with |
| 226 | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
| 227 | |
| 228 | """ |
| 229 | # cached encoder |
| 230 | if (not skipkeys and ensure_ascii and |
| 231 | check_circular and allow_nan and |
| 232 | cls is None and indent is None and separators is None and |
| 233 | default is None and not sort_keys and not kw): |
| 234 | return _default_encoder.encode(obj) |
| 235 | if cls is None: |
| 236 | cls = JSONEncoder |
| 237 | return cls( |
| 238 | skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 239 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
| 240 | separators=separators, default=default, sort_keys=sort_keys, |
| 241 | **kw).encode(obj) |