Extensible JSON encoder for Python data structures. Supports the following objects and types by default: +-------------------+---------------+ | Python | JSON | +===================+===============+ | dict, namedtuple | object |
| 123 | c_encode_basestring or py_encode_basestring) |
| 124 | |
| 125 | class JSONEncoder(object): |
| 126 | """Extensible JSON <http://json.org> encoder for Python data structures. |
| 127 | |
| 128 | Supports the following objects and types by default: |
| 129 | |
| 130 | +-------------------+---------------+ |
| 131 | | Python | JSON | |
| 132 | +===================+===============+ |
| 133 | | dict, namedtuple | object | |
| 134 | +-------------------+---------------+ |
| 135 | | list, tuple | array | |
| 136 | +-------------------+---------------+ |
| 137 | | str, unicode | string | |
| 138 | +-------------------+---------------+ |
| 139 | | int, long, float | number | |
| 140 | +-------------------+---------------+ |
| 141 | | True | true | |
| 142 | +-------------------+---------------+ |
| 143 | | False | false | |
| 144 | +-------------------+---------------+ |
| 145 | | None | null | |
| 146 | +-------------------+---------------+ |
| 147 | |
| 148 | To extend this to recognize other objects, subclass and implement a |
| 149 | ``.default()`` method with another method that returns a serializable |
| 150 | object for ``o`` if possible, otherwise it should call the superclass |
| 151 | implementation (to raise ``TypeError``). |
| 152 | |
| 153 | """ |
| 154 | item_separator = ', ' |
| 155 | key_separator = ': ' |
| 156 | |
| 157 | def __init__(self, skipkeys=False, ensure_ascii=True, |
| 158 | check_circular=True, allow_nan=False, sort_keys=False, |
| 159 | indent=None, separators=None, encoding='utf-8', default=None, |
| 160 | use_decimal=True, namedtuple_as_object=True, |
| 161 | tuple_as_array=True, bigint_as_string=False, |
| 162 | item_sort_key=None, for_json=False, ignore_nan=False, |
| 163 | int_as_string_bitcount=None, iterable_as_array=False): |
| 164 | """Constructor for JSONEncoder, with sensible defaults. |
| 165 | |
| 166 | If skipkeys is false, then it is a TypeError to attempt |
| 167 | encoding of keys that are not str, int, long, float or None. If |
| 168 | skipkeys is True, such items are simply skipped. |
| 169 | |
| 170 | If ensure_ascii is true, the output is guaranteed to be str |
| 171 | objects with all incoming unicode characters escaped. If |
| 172 | ensure_ascii is false, the output will be unicode object. |
| 173 | |
| 174 | If check_circular is true, then lists, dicts, and custom encoded |
| 175 | objects will be checked for circular references during encoding to |
| 176 | prevent an infinite recursion (which would cause an OverflowError). |
| 177 | Otherwise, no such check takes place. |
| 178 | |
| 179 | If allow_nan is true (default: False), then out of range float |
| 180 | values (nan, inf, -inf) will be serialized to |
| 181 | their JavaScript equivalents (NaN, Infinity, -Infinity) |
| 182 | instead of raising a ValueError. See |
no outgoing calls
no test coverage detected
searching dependent graphs…