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:
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:
Reading from an open file:
>>> import jsonyx as json >>> from io import StringIO >>> decoder = json.Decoder() >>> io = StringIO('["streaming API"]') >>> decoder.load(io) ['streaming API']
Reading from a file:
>>> 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('["streaming API"]') ... with open(filename, "rb") as fp: ... decoder.load(fp) ... ['streaming API']
Tip
Specify
rootto display the zip filename in error messages.Note
The encoding is detected using
jsonyx.detect_encoding().
- 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.