41
41
42
42
# stdlib
43
43
import contextlib
44
+ import gzip
44
45
import json
45
46
import os
46
47
import pathlib
@@ -543,17 +544,20 @@ def dump_json(
543
544
encoding : Optional [str ] = "UTF-8" ,
544
545
errors : Optional [str ] = None ,
545
546
json_library : JsonLibrary = json , # type: ignore
547
+ * ,
548
+ compress : bool = False ,
546
549
** kwargs ,
547
550
) -> None :
548
- """
551
+ r """
549
552
Dump ``data`` to the file as JSON.
550
553
551
554
:param data: The object to serialise to JSON.
552
555
:param encoding: The encoding to write to the file in.
553
556
:param errors:
554
557
:param json_library: The JSON serialisation library to use.
555
558
: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.
557
561
558
562
.. versionadded:: 0.5.0
559
563
@@ -562,8 +566,16 @@ def dump_json(
562
566
Now uses :meth:`PathPlus.write_clean <domdf_python_tools.paths.PathPlus.write_clean>`
563
567
rather than :meth:`PathPlus.write_text <domdf_python_tools.paths.PathPlus.write_text>`,
564
568
and returns :py:obj:`None` rather than :class:`int`.
569
+
570
+ .. versionchanged:: 1.8.0
571
+
572
+ Added the ``compress`` keyword-only argument.
565
573
"""
566
574
575
+ if compress :
576
+ with gzip .open (self , mode = "wt" , encoding = encoding , errors = errors ) as fp :
577
+ return fp .write (json_library .dumps (data , ** kwargs ))
578
+
567
579
return self .write_clean (
568
580
json_library .dumps (data , ** kwargs ),
569
581
encoding = encoding ,
@@ -575,24 +587,38 @@ def load_json(
575
587
encoding : Optional [str ] = "UTF-8" ,
576
588
errors : Optional [str ] = None ,
577
589
json_library : JsonLibrary = json , # type: ignore
590
+ * ,
591
+ decompress : bool = False ,
578
592
** kwargs ,
579
593
) -> Any :
580
- """
594
+ r """
581
595
Load JSON data from the file.
582
596
583
597
:param encoding: The encoding to write to the file in.
584
598
:param errors:
585
599
:param json_library: The JSON serialisation library to use.
586
600
:default json_library: :mod:`json`
587
- :param kwargs: Keyword arguments to pass to the JSON deserialisation function.
601
+ :param decompress: Whether to decompress the JSON file using gzip.
602
+ Will raise an exception if the file is not compressed.
603
+ :param \*\*kwargs: Keyword arguments to pass to the JSON deserialisation function.
588
604
589
605
:return: The deserialised JSON data.
590
606
591
607
.. versionadded:: 0.5.0
608
+
609
+ .. versionchanged:: 1.8.0
610
+
611
+ Added the ``compress`` keyword-only argument.
592
612
"""
593
613
614
+ if decompress :
615
+ with gzip .open (self , mode = "rt" , encoding = encoding , errors = errors ) as fp :
616
+ content = fp .read ()
617
+ else :
618
+ content = self .read_text (encoding = encoding , errors = errors )
619
+
594
620
return json_library .loads (
595
- self . read_text ( encoding = encoding , errors = errors ) ,
621
+ content ,
596
622
** kwargs ,
597
623
)
598
624
0 commit comments