-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez_json.py
157 lines (115 loc) · 4.85 KB
/
ez_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import json
from typing import Any, Iterator, Tuple
def loadJson(path):
try:
with open(path, "r", encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
print(f'Warning: The file at "{path}" was not found. Created new empty dict.')
return {}
except json.JSONDecodeError:
print(f'Error: The file at "{path}" could not be decoded as JSON. Created new empty dict.')
return {}
except Exception as e:
print(f'An unexpected error occurred while loading JSON from "{path}": {e}. Created new empty dict.')
return {}
def writeJson(data, path):
try:
with open(path, 'w+', encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
except TypeError as e:
print(f"Error: The provided data is not serializable to JSON. {e}")
except IOError as e:
print(f"Error: An I/O error occurred while writing JSON to {path}. {e}")
except Exception as e:
print(f"An unexpected error occurred while writing JSON to {path}: {e}")
class JSON:
"""
## EZ - JSON
EZ - JSON provides tools for managing JSON files, simplifying the process of reading, saving and manipulating data stored in JSON format.
"""
def __init__(self, filepath: str, default_value: any = None):
self.filepath = filepath
self.default_value = default_value
self.data = loadJson(filepath)
def save(self, path: str = None) -> None:
"""
Save the current JSON data to a file.
### Parameters:
- path (str): The path to the file where the JSON data will be saved. If not provided, the data will be saved to the file path provided during initialization.
"""
writeJson(self.data, path if path else self.filepath)
def overwrite(self, data: dict) -> None:
"""
Replace the current JSON data with the provided data.
### Parameters:
- data (dict): The new data to replace the current JSON data with.
"""
self.data = data
def read(self) -> dict:
"""
This function retrieves the current JSON data stored in the file specified during initialization.
### Returns:
- The function returns the current JSON data as a dictionary.
"""
return self.data
def _traverse_keys(self, key: str, create_missing: bool = True) -> tuple:
keys = key.split('.')
last_key = keys[-1]
current_dict = self.data
for k in keys[:-1]:
if isinstance(current_dict, list) and k.isdigit():
k = int(k)
if k < len(current_dict):
current_dict = current_dict[k]
else:
raise IndexError(f"Index {k} is out of range for the list.")
elif isinstance(current_dict, dict):
if create_missing and k not in current_dict:
current_dict[k] = {}
current_dict = current_dict.get(k, {})
else:
raise KeyError(f"Key '{k}' does not exist or is not a dictionary.")
return current_dict, last_key
def __getitem__(self, key: str) -> Any:
default = self.default_value
keys = key.split('.')
current_dict = self.data
for k in keys:
if k in current_dict:
current_dict = current_dict[k]
else:
return default
return current_dict
def __setitem__(self, key: str, value: Any) -> None:
current_dict, last_key = self._traverse_keys(key)
current_dict[last_key] = value
def __delitem__(self, key: str) -> None:
current_dict, last_key = self._traverse_keys(key)
if last_key in current_dict:
del current_dict[last_key]
def __str__(self) -> str:
return json.dumps(self.data, indent = 4, ensure_ascii = False)
def __contains__(self, key: str) -> bool:
keys = key.split('.')
current_dict = self.data
for k in keys:
if isinstance(current_dict, dict) and k in current_dict:
current_dict = current_dict[k]
else:
return False
return True
def items(self) -> Iterator[Tuple[str, Any]]:
"""
This function retrieves the key-value pairs of the current JSON data stored in the file specified during initialization.
### Returns:
- The function returns an iterator that yields each key-value pair in the JSON data as a tuple.
"""
return self.data.items()
# Example usage
if __name__ == '__main__':
json_obj = JSON('NewJson.json')
json_obj.data
del json_obj['Chips1.Chips2']
json_obj.save()
print(json_obj)