jsonyx.write¶
- jsonyx.write(obj, filename, encoding='utf-8', *, allow=NOTHING, check_circular=True, commas=True, end='\\n', ensure_ascii=False, formatters=None, hook=None, indent=None, indent_leaves=True, max_indent_level=None, quoted_keys=True, separators=(', ', ': '), skipkeys=False, sort_keys=False, trailing_comma=False, types=None)[source]¶
Serialize a Python object to a JSON file.
Changed in version 2.0:
Added
commas,encoding,indent_leaves,max_indent_level,quoted_keysandtypes.Allowed overriding serialization in subclasses of
floatandint(e.g.enum.Enum).Made
tupleserializable by default instead ofdecimal.Decimal.Replaced
item_separatorandkey_separatorwithseparators.
Changed in version 2.1: Added
check_circular,hookandskipkeys.Changed in version 2.4:
Added
formatters.Allowed overriding serialization in subclasses of
str(e.g.enum.Enum).Made
frozendictserializable by default.
- Parameters:
obj (
object) – a Python objectfilename (_StrPath) – the path to the JSON file
encoding (
str, default:"utf-8") – the JSON encodingallow (
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 indentedend (
str, default:"\\n") – the string to append at the endensure_ascii (
bool, default:False) – escape non-ASCII charactersformatters (
dict[str, _Formatter] |None, default:None) – a dictionary of formattershook (_Hook |
None, default:None) – the hook used for transforming dataindent (
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, _ClassInfo] |None, default:None) – a dictionary of additional types
- 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 os.path import join >>> from tempfile import TemporaryDirectory >>> with TemporaryDirectory() as tmpdir: ... filename = join(tmpdir, "file.json") ... json.write(["filesystem API"], filename) ... with open(filename, "r", encoding="utf-8") as fp: ... fp.read() ... '["filesystem API"]\n'
Tip
Use
jsonyx.Encoderdirectly for better performance.Note
The item separator is automatically stripped when indented.
The output is not written incrementally, but in one-shot.
Warning
Avoid specifying ABCs for
types, that is very slow.See also
jsonyx.dump()for serializing to an open file.jsonyx.dumps()for serializing to a string.jsonyx.read()for deserializing from a file.