Pelokalan
Python ID
id en
Masuk
Proyek
library__json.po Berkas proyek · python-docs-id-3.14 · Bahasa Indonesia
Modul PO Hanya lihat
Teks sumber — English
Terjemahan — Bahasa Indonesia
>>> import json >>> class ComplexEncoder(json.JSONEncoder): ... def default(self, obj): ... if isinstance(obj, complex): ... return [obj.real, obj.imag] ... # Let the base class default method raise the TypeError ... return super().default(obj) ... >>> json.dumps(2 + 1j, cls=ComplexEncoder) '[2.0, 1.0]' >>> ComplexEncoder().encode(2 + 1j) '[2.0, 1.0]' >>> list(ComplexEncoder().iterencode(2 + 1j)) ['[2.0', ', 1.0', ']']
If set, a function that is called with the result of any JSON object literal decoded with an ordered list of pairs. The return value of this function will be used instead of the :class:`dict`. This feature can be used to implement custom decoders. If *object_hook* is also set, *object_pairs_hook* takes priority. Default ``None``.
If set, a function that is called with the result of any JSON array literal decoded as a Python list. The return value of this function will be used instead of the :class:`list`. This feature can be used to implement custom decoders. Default ``None``.
list
list
*object_pairs_hook* is an optional function that will be called with the result of every JSON object decoded with an ordered list of pairs. The return value of *object_pairs_hook* will be used instead of the :class:`dict`. This feature can be used to implement custom decoders. If *object_hook* is also defined, the *object_pairs_hook* takes priority.
*array_hook* is an optional function that will be called with the result of every JSON array decoded as a list. The return value of *array_hook* will be used instead of the :class:`list`. This feature can be used to implement custom decoders.
list, tuple
list, tuple
If *check_circular* is true (the default), then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause a :exc:`RecursionError`). Otherwise, no such check takes place.
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 super().default(o)
The old version of JSON specified by the obsolete :rfc:`4627` required that the top-level value of a JSON text must be either a JSON object or array (Python :class:`dict` or :class:`list`), and could not be a JSON null, boolean, number, or string value. :rfc:`7159` removed that restriction, and this module does not and has never implemented that restriction in either its serializer or its deserializer.