Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). 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 ``en
(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None,
default=None, sort_keys=False, **kw)
| 117 | ) |
| 118 | |
| 119 | def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, |
| 120 | allow_nan=True, cls=None, indent=None, separators=None, |
| 121 | default=None, sort_keys=False, **kw): |
| 122 | """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a |
| 123 | ``.write()``-supporting file-like object). |
| 124 | |
| 125 | If ``skipkeys`` is true then ``dict`` keys that are not basic types |
| 126 | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
| 127 | instead of raising a ``TypeError``. |
| 128 | |
| 129 | If ``ensure_ascii`` is false, then the strings written to ``fp`` can |
| 130 | contain non-ASCII and non-printable characters if they appear in strings |
| 131 | contained in ``obj``. Otherwise, all such characters are escaped in JSON |
| 132 | strings. |
| 133 | |
| 134 | If ``check_circular`` is false, then the circular reference check |
| 135 | for container types will be skipped and a circular reference will |
| 136 | result in an ``RecursionError`` (or worse). |
| 137 | |
| 138 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
| 139 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) |
| 140 | in strict compliance of the JSON specification, instead of using the |
| 141 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 142 | |
| 143 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 144 | object members will be pretty-printed with that indent level. An indent |
| 145 | level of 0 will only insert newlines. ``None`` is the most compact |
| 146 | representation. |
| 147 | |
| 148 | If specified, ``separators`` should be an ``(item_separator, |
| 149 | key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is |
| 150 | ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON |
| 151 | representation, you should specify ``(',', ':')`` to eliminate |
| 152 | whitespace. |
| 153 | |
| 154 | ``default(obj)`` is a function that should return a serializable version |
| 155 | of obj or raise TypeError. The default simply raises TypeError. |
| 156 | |
| 157 | If *sort_keys* is true (default: ``False``), then the output of |
| 158 | dictionaries will be sorted by key. |
| 159 | |
| 160 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 161 | ``.default()`` method to serialize additional types), specify it with |
| 162 | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
| 163 | |
| 164 | """ |
| 165 | # cached encoder |
| 166 | if (not skipkeys and ensure_ascii and |
| 167 | check_circular and allow_nan and |
| 168 | cls is None and indent is None and separators is None and |
| 169 | default is None and not sort_keys and not kw): |
| 170 | iterable = _default_encoder.iterencode(obj) |
| 171 | else: |
| 172 | if cls is None: |
| 173 | cls = JSONEncoder |
| 174 | iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 175 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
| 176 | separators=separators, |
nothing calls this directly
no test coverage detected
searching dependent graphs…