1
- """
2
- This module provides a JsonWrapper class for handling JSON file operations.
3
- It includes functionality for reading, writing, and managing JSON files.
4
- """
5
-
6
1
import json
7
2
8
3
9
4
class JsonWrapper :
10
- """
11
- A utility class for reading, writing, and managing JSON files.
12
- Provides methods for reading JSON and text files, writing JSON data,
13
- and clearing JSON file contents.
14
- """
15
5
@staticmethod
16
6
def read_json (file_path ):
17
7
"""
@@ -23,7 +13,7 @@ def read_json(file_path):
23
13
str: The contents of the JSON file as a pretty-printed JSON string.
24
14
"""
25
15
26
- with open (file_path , 'r' , encoding = 'utf-8' ) as file :
16
+ with open (file_path , 'r' ) as file :
27
17
data = json .load (file )
28
18
return json .dumps (data , indent = 4 )
29
19
@@ -36,7 +26,7 @@ def read_txt(self, file_path):
36
26
str: The contents of the text file as a string.
37
27
"""
38
28
39
- with open (file_path , 'r' , encoding = 'utf-8' ) as file :
29
+ with open (file_path , 'r' ) as file :
40
30
return file .read ()
41
31
42
32
@staticmethod
@@ -51,7 +41,7 @@ def write(file_path, data):
51
41
IOError: If the file cannot be opened or written to.
52
42
"""
53
43
54
- with open (file_path , 'a' , encoding = 'utf-8' ) as file :
44
+ with open (file_path , 'a' ) as file :
55
45
json .dump (data , file , indent = 4 )
56
46
57
47
@staticmethod
@@ -64,7 +54,7 @@ def delete(file_path):
64
54
IOError: If the file cannot be deleted.
65
55
"""
66
56
67
- with open (file_path , 'r' , encoding = 'utf-8' ) as file :
57
+ with open (file_path , 'r' ) as file :
68
58
data = json .load (file )
69
59
70
60
if isinstance (data , list ):
@@ -75,5 +65,5 @@ def delete(file_path):
75
65
raise ValueError (
76
66
"The file does not contain a JSON object or array." )
77
67
78
- with open (file_path , 'w' , encoding = 'utf-8' ) as file :
68
+ with open (file_path , 'w' ) as file :
79
69
json .dump (empty_data , file , indent = 4 )
0 commit comments