-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_use.py
67 lines (54 loc) · 1.61 KB
/
json_use.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Class to simplify the use and access of JSON files.
"""
import json
class JSONUse(object):
"""Class to simplify the use and access of JSON files."""
def __init__(self, file=None):
super(JSONUse, self).__init__()
self.file = file
self.json_data = None
def serialize(self, data, own_decoder=None):
"""
Serializes the information that receives:
- If it not receives file, return the one generated.
- If not get, same result.
- Enable own decoder.
"""
if self.file:
json.dump(data, self.file, indent = 4, default = own_decoder)
self.json_data = json.dumps(data, indent = 4, default = own_decoder)
def deserialize(self):
"""
Deserialize info.
- If there is a file, from it.
- Else, from a class member.
"""
if self.file:
return json.load(self.file)
else:
return json.loads(self.json_data)
def main():
json_string = """
{
"researcher": {
"name": "Ford Prefect",
"species": "Betelgeusian",
"relatives": [
{
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian"
}
]
}
}
"""
file = open("test.json", "w")
json_object = JSONUse(file)
json_object.serialize(json_string)
print(json_object.json_data)
file.close()
if __name__ == "__main__":
main()