json¶
JSON encoder and decoder.
Functions¶
json.dumps(obj: object | None) -> str¶
Serialize obj to a JSON formatted string.
Parameters:
obj(object | None) -- The object to serialize.
Returns: A JSON string representation of obj.
json.dumps(obj: object | None, indent: int = -1, sort_keys: bool = False, ensure_ascii: bool = True, separators: tuple[str, str] | None = None, default: ((object) -> object | None) | None = None, cls: JSONEncoder | None = None, allow_nan: bool = True) -> str¶
Serialize obj to a JSON formatted string with formatting options.
Parameters:
obj(object | None) -- The object to serialize.indent(int) -- Number of spaces for indentation. Use -1 for compact output.sort_keys(bool) -- Whether to sort dictionary keys.ensure_ascii(bool) -- Whether to escape non-ASCII characters.separators(tuple[str, str] | None) -- A tuple of(itemSeparator, keySeparator)overriding the defaults. WhenNone, defaults to(", ", ": ")in compact mode and(",", ": ")-style behavior in pretty mode (newlines drive item separation).default(((object) -> object | None) | None) -- Optional callback invoked for any value that is not natively JSON-serializable. The callback should return a JSON-serializable replacement value, or raise aTypeErrorfor unsupported types. Returning the original object will raise aTypeErrorto avoid infinite recursion. Nameddefaultfor Python compatibility (kwargdefault=).cls(JSONEncoder | None) -- OptionalJSONEncoderinstance. When provided, delegates serialization tocls.Encode(obj).allow_nan(bool) -- WhenTrue(CPython's default),Infinity,-InfinityandNaNare emitted as those literal tokens — CPython's own extension to JSON, which its parser also accepts. WhenFalse, a non-finite float raisesValueError(#1296).
Returns: A JSON string representation of obj.
json.loads(s: str, cls: JSONDecoder | None = None, object_hook: ((dict[str, object | None]) -> object | None) | None = None) -> object | None¶
Deserialize a JSON string to a Python-like object. Returns Dict for objects, List for arrays, string, int/long/double, bool, or None.
Parameters:
s(str) -- The JSON string to deserialize.cls(JSONDecoder | None) -- OptionalJSONDecoderinstance for custom decoding.object_hook(((dict[str, object | None]) -> object | None) | None) -- Optional callback invoked for every decoded JSON object (dict).
Returns: The deserialized object.
json.dump(obj: object | None, fp: TextFile)¶
Serialize obj as a JSON formatted stream to a file.
Parameters:
obj(object | None) -- The object to serialize.fp(TextFile) -- The file to write to.
json.dump(obj: object | None, fp: TextFile, indent: int = -1, sort_keys: bool = False, ensure_ascii: bool = True, separators: tuple[str, str] | None = None, default: ((object) -> object | None) | None = None, cls: JSONEncoder | None = None, allow_nan: bool = True)¶
Serialize obj as a JSON formatted stream to a file with formatting options.
Parameters:
obj(object | None) -- The object to serialize.fp(TextFile) -- The file to write to.indent(int) -- Number of spaces for indentation. Use -1 for compact output.sort_keys(bool) -- Whether to sort dictionary keys.ensure_ascii(bool) -- Whether to escape non-ASCII characters.separators(tuple[str, str] | None) -- A tuple of(itemSeparator, keySeparator)overriding the defaults. Seedumps.default(((object) -> object | None) | None) -- Optional callback invoked for any value that is not natively JSON-serializable. Seedumps.cls(JSONEncoder | None) -- OptionalJSONEncoderinstance for custom encoding.allow_nan(bool) -- WhenTrue(CPython's default), non-finite floats are emitted asInfinity/-Infinity/NaN; whenFalsethey raiseValueError(#1296).
json.load(fp: TextFile, cls: JSONDecoder | None = None, object_hook: ((dict[str, object | None]) -> object | None) | None = None) -> object | None¶
Deserialize a JSON document read from a file.
Parameters:
fp(TextFile) -- The file to read from.cls(JSONDecoder | None) -- OptionalJSONDecoderinstance for custom decoding.object_hook(((dict[str, object | None]) -> object | None) | None) -- Optional callback invoked for every decoded JSON object (dict).
Returns: The deserialized object.
json.loads(s: str) -> Result[T, JSONDecodeError]¶
Deserialize a JSON string to a strongly-typed object using System.Text.Json.
Parameters:
s(str) -- The JSON string to deserialize.
Returns: A Result[T, E] containing the deserialized value on success,
or a JSONDecodeError on failure.
json.load(fp: TextFile) -> Result[T, JSONDecodeError]¶
Deserialize a JSON document read from a file to a strongly-typed object.
Parameters:
fp(TextFile) -- The file to read from.
Returns: A Result[T, E] containing the deserialized value on success,
or a JSONDecodeError on failure.
JSONDecodeError¶
Subclass of ValueError raised when a JSON document cannot be decoded. Mirrors Python's json.JSONDecodeError.
Properties¶
| Name | Type | Description |
|---|---|---|
msg |
str |
The unformatted error message. |
doc |
str |
The JSON document being parsed. |
pos |
int |
The index in doc where parsing failed. |
JSONDecoder¶
Simple JSON decoder. Subclass this to customize JSON decoding behavior.
decode(s: str) -> object | None¶
Deserialize a JSON string to a Python-like object.
Parameters:
s(str) -- The JSON string to deserialize.
Returns: The deserialized object.
raw_decode(s: str, idx: int = 0) -> tuple[object | None, int]¶
Decode a JSON document from a string, starting at the given index.
Parameters:
s(str) -- The JSON string to decode.idx(int) -- The index in the string at which to begin decoding.
Returns: A tuple of the decoded object and the index where the document ended.
JSONEncoder¶
Extensible JSON encoder. Subclass this to customize JSON encoding behavior.
default(obj: object) -> object | None¶
Called for objects that cannot otherwise be serialized. Override to provide custom serialization.
Parameters:
obj(object) -- The object that is not JSON serializable.
Returns: A JSON-serializable replacement object.
encode(obj: object | None) -> str¶
Serialize an object to a JSON formatted string.
Parameters:
obj(object | None) -- The object to serialize.
Returns: A JSON string representation of the object.