jsonyx.Decoder¶
- class jsonyx.Decoder(*, allow=NOTHING, cache_keys=False, hooks=None)¶
Bases:
objectA configurable JSON decoder.
Changed in version 2.0: Replaced
use_decimalwithhooks.`Changed in version 2.2: Added
cache_keys(defaultFalseinstead ofTrue).- Parameters:
See also
jsonyx.Encoderfor serialization.- load(fp, *, root='.')[source]¶
Deserialize an open JSON file to a Python object.
- Parameters:
- Raises:
RecursionError – if the JSON file is too deeply nested
TruncatedSyntaxError – when failing to decode the file
ValueError – if a number is too big
- Returns:
Any– a Python object
- Example:
>>> import jsonyx as json >>> from io import StringIO >>> decoder = json.Decoder() >>> io = StringIO('["reader protocol"]') >>> decoder.load(io) ['reader protocol']
Tip
Specify
rootto display the zip filename in error messages.Note
The input is not read incrementally, but in one-shot.
The encoding is detected using
jsonyx.detect_encoding().Keys are never re-used across calls.
- loads(s, *, filename='<string>')[source]¶
Deserialize a JSON string to a Python object.
- Parameters:
- Raises:
RecursionError – if the JSON string is too deeply nested
TruncatedSyntaxError – when failing to decode the string
ValueError – if a number is too big
- Returns:
Any– a Python object
- Example:
>>> import jsonyx as json >>> decoder = json.Decoder() >>> decoder.loads('{"foo": ["bar", null, 1.0, 2]}') {'foo': ['bar', None, 1.0, 2]} >>> decoder.loads(r'"\"foo\bar"') '"foo\x08ar'
Tip
Specify
filenameto display the filename in error messages.Note
The encoding is detected using
jsonyx.detect_encoding().Keys are never re-used across calls.
- read(filename)[source]¶
Deserialize a JSON file to a Python object.
- Parameters:
filename (_StrPath) – the path to the JSON file
- Raises:
OSError – if the file can’t be opened
RecursionError – if the JSON file is too deeply nested
TruncatedSyntaxError – when failing to decode the file
ValueError – if a number is too big
- Returns:
Any– a Python object
- Example:
>>> import jsonyx as json >>> from os.path import join >>> from tempfile import TemporaryDirectory >>> decoder = json.Decoder() >>> with TemporaryDirectory() as tmpdir: ... filename = join(tmpdir, "file.json") ... with open(filename, "w", encoding="utf-8") as fp: ... _ = fp.write('["filesystem API"]') ... decoder.read(filename) ... ['filesystem API']
Note
The input is not read incrementally, but in one-shot.
The encoding is detected using
jsonyx.detect_encoding().Keys are never re-used across calls.