💾 Archived View for tris.fyi › pydoc › json.encoder captured on 2023-04-26 at 13:36:40. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-01-29)
-=-=-=-=-=-=-
Implementation of JSONEncoder
Extensible JSON <https://json.org> encoder for Python data structures. Supports the following objects and types by default: +-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ | str | string | +-------------------+---------------+ | int, float | number | +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+ To extend this to recognize other objects, subclass and implement a ``.default()`` method with another method that returns a serializable object for ``o`` if possible, otherwise it should call the superclass implementation (to raise ``TypeError``).
default(self, o) Implement this method in a subclass such that it returns a serializable object for ``o``, or calls the base implementation (to raise a ``TypeError``). For example, to support arbitrary iterators, you could implement default like this:: def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
encode(self, o) Return a JSON string representation of a Python data structure. >>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}'
iterencode(self, o, _one_shot=False) Encode the given object and yield each string representation as available. For example:: for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk)
item_separator = ', '
key_separator = ': '
_iterencode(obj, _current_indent_level) -> iterable
default = <member 'default' of '_json.Encoder' objects> default
encoder = <member 'encoder' of '_json.Encoder' objects> encoder
indent = <member 'indent' of '_json.Encoder' objects> indent
item_separator = <member 'item_separator' of '_json.Encoder' objects> item_separator
key_separator = <member 'key_separator' of '_json.Encoder' objects> key_separator
markers = <member 'markers' of '_json.Encoder' objects> markers
skipkeys = <member 'skipkeys' of '_json.Encoder' objects> skipkeys
sort_keys = <member 'sort_keys' of '_json.Encoder' objects> sort_keys
encode_basestring(...) encode_basestring(string) -> string Return a JSON representation of a Python string
encode_basestring_ascii(...) encode_basestring_ascii(string) -> string Return an ASCII-only JSON representation of a Python string
encode_basestring(...) encode_basestring(string) -> string Return a JSON representation of a Python string
encode_basestring_ascii(...) encode_basestring_ascii(string) -> string Return an ASCII-only JSON representation of a Python string
py_encode_basestring(s) Return a JSON representation of a Python string
py_encode_basestring_ascii(s) Return an ASCII-only JSON representation of a Python string
ESCAPE = re.compile('[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t]')
ESCAPE_ASCII = re.compile('([\\\\"]|[^\\ -~])')
ESCAPE_DCT = {'\\': '\\\\', '"': '\\"', '\x08': '\\b', '\x0c': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t', '\x00': '\\u0000', '\x01': '\\u0001', '\x02': '\\u0002', '\x03': '\\u0003', '\x04': '\\u0004', '\x05': '\\u0005', '\x06': '\\u0006', '\x07': '\\u0007', '\x0b': '\\u000b', '\x0e': '\\u000e', '\x0f': '\\u000f', '\x10': '\\u0010', '\x11': '\\u0011', '\x12': '\\u0012', '\x13': '\\u0013', '\x14': '\\u0014', '\x15': '\\u0015', '\x16': '\\u0016', '\x17': '\\u0017', '\x18': '\\u0018', '\x19': '\\u0019', '\x1a': '\\u001a', '\x1b': '\\u001b', '\x1c': '\\u001c', '\x1d': '\\u001d', '\x1e': '\\u001e', '\x1f': '\\u001f'}
HAS_UTF8 = re.compile(b'[\x80-\xff]')
INFINITY = inf
i = 31