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,37 +566,60 @@ 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.9.0
571
+
572
+ Added the ``compress`` keyword-only argument.
565
573
"""
566
574
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
+ )
572
585
573
586
def load_json (
574
587
self ,
575
588
encoding : Optional [str ] = "UTF-8" ,
576
589
errors : Optional [str ] = None ,
577
590
json_library : JsonLibrary = json , # type: ignore
591
+ * ,
592
+ decompress : bool = False ,
578
593
** kwargs ,
579
594
) -> Any :
580
- """
595
+ r """
581
596
Load JSON data from the file.
582
597
583
598
:param encoding: The encoding to write to the file in.
584
599
:param errors:
585
600
:param json_library: The JSON serialisation library to use.
586
601
: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.
588
605
589
606
:return: The deserialised JSON data.
590
607
591
608
.. versionadded:: 0.5.0
609
+
610
+ .. versionchanged:: 1.9.0
611
+
612
+ Added the ``compress`` keyword-only argument.
592
613
"""
593
614
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
+
594
621
return json_library .loads (
595
- self . read_text ( encoding = encoding , errors = errors ) ,
622
+ content ,
596
623
** kwargs ,
597
624
)
598
625
0 commit comments