4141
4242# stdlib
4343import contextlib
44+ import gzip
4445import json
4546import os
4647import pathlib
@@ -543,17 +544,20 @@ def dump_json(
543544 encoding : Optional [str ] = "UTF-8" ,
544545 errors : Optional [str ] = None ,
545546 json_library : JsonLibrary = json , # type: ignore
547+ * ,
548+ compress : bool = False ,
546549 ** kwargs ,
547550 ) -> None :
548- """
551+ r """
549552 Dump ``data`` to the file as JSON.
550553
551554 :param data: The object to serialise to JSON.
552555 :param encoding: The encoding to write to the file in.
553556 :param errors:
554557 :param json_library: The JSON serialisation library to use.
555558 :default json_library: :mod:`json`
556- :param kwargs: Keyword arguments to pass to the JSON serialisation function.
559+ :param compress: Whether to compress the JSON file using gzip.
560+ :param \*\*kwargs: Keyword arguments to pass to the JSON serialisation function.
557561
558562 .. versionadded:: 0.5.0
559563
@@ -562,37 +566,60 @@ def dump_json(
562566 Now uses :meth:`PathPlus.write_clean <domdf_python_tools.paths.PathPlus.write_clean>`
563567 rather than :meth:`PathPlus.write_text <domdf_python_tools.paths.PathPlus.write_text>`,
564568 and returns :py:obj:`None` rather than :class:`int`.
569+
570+ .. versionchanged:: 1.9.0
571+
572+ Added the ``compress`` keyword-only argument.
565573 """
566574
567- return self .write_clean (
568- json_library .dumps (data , ** kwargs ),
569- encoding = encoding ,
570- errors = errors ,
571- )
575+ if compress :
576+ with gzip .open (self , mode = "wt" , encoding = encoding , errors = errors ) as fp :
577+ fp .write (json_library .dumps (data , ** kwargs ))
578+
579+ else :
580+ self .write_clean (
581+ json_library .dumps (data , ** kwargs ),
582+ encoding = encoding ,
583+ errors = errors ,
584+ )
572585
573586 def load_json (
574587 self ,
575588 encoding : Optional [str ] = "UTF-8" ,
576589 errors : Optional [str ] = None ,
577590 json_library : JsonLibrary = json , # type: ignore
591+ * ,
592+ decompress : bool = False ,
578593 ** kwargs ,
579594 ) -> Any :
580- """
595+ r """
581596 Load JSON data from the file.
582597
583598 :param encoding: The encoding to write to the file in.
584599 :param errors:
585600 :param json_library: The JSON serialisation library to use.
586601 :default json_library: :mod:`json`
587- :param kwargs: Keyword arguments to pass to the JSON deserialisation function.
602+ :param decompress: Whether to decompress the JSON file using gzip.
603+ Will raise an exception if the file is not compressed.
604+ :param \*\*kwargs: Keyword arguments to pass to the JSON deserialisation function.
588605
589606 :return: The deserialised JSON data.
590607
591608 .. versionadded:: 0.5.0
609+
610+ .. versionchanged:: 1.9.0
611+
612+ Added the ``compress`` keyword-only argument.
592613 """
593614
615+ if decompress :
616+ with gzip .open (self , mode = "rt" , encoding = encoding , errors = errors ) as fp :
617+ content = fp .read ()
618+ else :
619+ content = self .read_text (encoding = encoding , errors = errors )
620+
594621 return json_library .loads (
595- self . read_text ( encoding = encoding , errors = errors ) ,
622+ content ,
596623 ** kwargs ,
597624 )
598625
0 commit comments