-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomJson.py
executable file
·156 lines (98 loc) · 3.86 KB
/
CustomJson.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
"""
Created on Mon Oct 23 19:19:59 2017
@author: Francisco J. Castellanos
@project name: DAMA
"""
import collections
import json
from file_manager import FileManager
class CustomJson:
dictionary = {}
def __init__(self):
self.dictionary = {}
def addPair(self, key, value):
self.dictionary[key] = value
def deleteKey(self, key):
self.dictionary.pop(key, None)
def __getitem__(self, key):
return self.dictionary.__getitem__(key)
def __setitem__(self, key, data):
self.dictionary.__setitem__(key, data)
def __delitem__(self, key):
self.dictionary.__delitem__(key)
def saveJson(self, path_file):
assert(type(path_file) is str)
content = json.dumps(self.dictionary, sort_keys=True, indent=4, separators=(',', ': '))
FileManager.saveString(content, path_file, True) #close_file
def convert(self, data):
try:
basestring
except NameError:
basestring = str
if isinstance(data, basestring):
return str(data)
if type(data) is dict:
return data
elif isinstance(data, collections.Mapping):
return dict(map(self.convert, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(self.convert, data))
else:
return data
def loadJson(self, path_file):
assert(type(path_file) is str)
content = FileManager.readStringFile(path_file)
self.dictionary = self.convert(json.loads(content))
def printJson (self):
print(json.dumps(self.dictionary))
def listToDictionary(lst):
assert(type(lst) is list)
dictionary = {}
count = 0
for item in lst:
if (type(item) is list):
dictionary[count] = CustomJson.listToDictionary(item)
elif type(item) is dict:
dictionary[count] = item
else:
assert(type(item) is str)
dictionary[count] = item
count = count + 1
return dictionary
listToDictionary = staticmethod(listToDictionary)
def listFromDictionary(dct):
assert(type(dct) is dict)
lst = []
for count in range(len(dct)):
item = dct[str(count)]
if (type(item) is dict):
lst.append(CustomJson.listFromDictionary(item))
else:
assert(type(item) is str or type(item) is unicode)
lst.append(str(item))
return lst
listFromDictionary = staticmethod(listFromDictionary)
def listFromDictionaryWithoutNumerate(dct):
assert(type(dct) is dict)
lst = []
for item in dct:
if (type(item) is dict):
lst.append(CustomJson.listFromDictionary(item))
else:
assert(type(item) is str or type(item) is unicode)
lst.append(str(item))
return lst
listFromDictionaryWithoutNumerate = staticmethod(listFromDictionaryWithoutNumerate)
#==============================================================================
# Code Tests...
#==============================================================================
if __name__ == "__main__":
json1 = CustomJson()
json1.addPair("person1", {"name": "Pepe", "dni": 66553})
json1.printJson()
json1.saveJson("tests/custom_json.txt")
json2 = CustomJson()
json2.loadJson("tests/custom_json.txt")
json2.printJson()
print(json2["person1"])
json2.saveJson("tests/custom_json2.txt")