json.JSONEncoder()
json.JSONEncoder() is a Python function in the JSON Module category. Extensible JSON encoder for custom serialization. The syntax is json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None). Returns: Encoder instance. A typical example: import json
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
return super().default(obj)
json.dumps(data, cls=CustomEncoder). A close sibling is json.dumps(), which serializes a Python object to a JSON formatted string. A close sibling is json.loads(), which deserializes a JSON string to a Python object. A close sibling is json.dump(), which serializes a Python object to a JSON formatted stream (file). A close sibling is json.load(), which deserializes a JSON formatted stream (file) to a Python object. More about this category: JSON encoding and decoding — dump, load, dumps, loads. For a broader walkthrough, read the JSON Module guide. Related JSON Module entries: json.dumps(), json.loads(), json.dump(), json.load(), json.JSONDecoder().