jsonyx.Encoder¶
- class jsonyx.Encoder(*, allow=NOTHING, check_circular=True, commas=True, hook=None, end='\\n', ensure_ascii=False, indent=None, indent_leaves=True, max_indent_level=None, quoted_keys=True, separators=(', ', ': '), skipkeys=False, sort_keys=False, trailing_comma=False, types=None)¶
Bases:
objectA configurable JSON encoder.
Changed in version 2.0:
Added
commas,indent_leaves,max_indent_level,quoted_keysandtypes.Made
tupleserializable by default instead ofenum.Enumanddecimal.Decimal.Replaced
item_separatorandkey_separatorwithseparators.
Changed in version 2.1: Added
check_circular,hookandskipkeys.- Parameters:
allow (
Container[str], default:NOTHING) – the JSON deviations fromjsonyx.allowcheck_circular (
bool, default:True) – check for circular referencescommas (
bool, default:True) – separate items by commas when indentedhook (_Hook |
None, default:None) – the hook used for transforming dataend (
str, default:"\\n") – the string to append at the endensure_ascii (
bool, default:False) – escape non-ASCII charactersindent (
int|str|None, default:None) – the number of spaces or string to indent withindent_leaves (
bool, default:True) – indent leaf objects and arraysmax_indent_level (
int|None, default:None) – the level up to which to indentquoted_keys (
bool, default:True) – quote keys which are identifiersseparators (
tuple[str,str], default:(", ", ": ")) – the item and key separatorskipkeys (
bool, default:False) – skip non-string keyssort_keys (
bool, default:False) – sort the keys of objectstrailing_comma (
bool, default:False) – add a trailing comma when indentedtypes (
dict[str,type|tuple[type,...]] |None, default:None) – a dictionary of additional types
Note
The item separator is automatically stripped when indented.
Warning
Avoid specifying ABCs for
types, that is very slow.- dump(obj, fp=None)[source]¶
Serialize a Python object to an open JSON file.
- Parameters:
- Raises:
RecursionError – if the object is too deeply nested
TypeError – for unserializable values
TruncatedSyntaxError – when failing to write to the file
ValueError – for invalid values
- Example:
Writing to standard output:
>>> import jsonyx as json >>> encoder = json.Encoder() >>> import jsonyx as json >>> encoder.dump('"foo\bar') "\"foo\bar" >>> encoder.dump("\\") "\\" >>> encoder.dump("\u20AC") "€"
Writing to an open file:
>>> import jsonyx as json >>> from io import StringIO >>> io = StringIO() >>> encoder.dump(["streaming API"], io) >>> io.getvalue() '["streaming API"]\n'
- dumps(obj)[source]¶
Serialize a Python object to a JSON string.
- Parameters:
obj (
object) – a Python object- Raises:
RecursionError – if the object is too deeply nested
TypeError – for unserializable values
ValueError – for invalid values
- Returns:
str– a JSON string
- Example:
>>> import jsonyx as json >>> encoder = json.Encoder() >>> encoder.dumps(["foo", {"bar": ("baz", None, 1.0, 2)}]) '["foo", {"bar": ["baz", null, 1.0, 2]}]\n'
- write(obj, filename, encoding='utf-8')[source]¶
Serialize a Python object to a JSON file.
Changed in version 2.0: Added
encoding.- Parameters:
- Raises:
OSError – if the file can’t be opened
RecursionError – if the object is too deeply nested
TypeError – for unserializable values
TruncatedSyntaxError – when failing to encode the file
ValueError – for invalid values
- Example:
>>> import jsonyx as json >>> from pathlib import Path >>> from tempfile import TemporaryDirectory >>> encoder = json.Encoder() >>> with TemporaryDirectory() as tmpdir: ... filename = Path(tmpdir) / "file.json" ... encoder.write(["filesystem API"], filename) ... filename.read_text("utf-8") ... '["filesystem API"]\n'