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.Disabled caching keys by default.
- Parameters:
- load(fp, *, root='.')[source]¶
Deserialize an open JSON file to a Python object.
- Parameters:
- Raises:
TruncatedSyntaxError – when failing to decode the file
- Returns:
Any– a Python object
- Example:
>>> import jsonyx as json >>> from io import StringIO >>> decoder = json.Decoder() >>> io = StringIO('["streaming API"]') >>> decoder.load(io) ['streaming API']
Tip
Specify
rootto display the zip filename in error messages.
- loads(s, *, filename='<string>')[source]¶
Deserialize a JSON string to a Python object.
- Parameters:
- Raises:
TruncatedSyntaxError – when failing to decode the string
- 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.
- 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
TruncatedSyntaxError – when failing to decode the file
- Returns:
Any– a Python object
- Example:
>>> import jsonyx as json >>> from pathlib import Path >>> from tempfile import TemporaryDirectory >>> decoder = json.Decoder() >>> with TemporaryDirectory() as tmpdir: ... filename = Path(tmpdir) / "file.json" ... _ = filename.write_text('["filesystem API"]', "utf-8") ... decoder.read(filename) ... ['filesystem API']