Skip to content

Commit 1a528ac

Browse files
authored
Update __init__.py
1 parent 88a54c9 commit 1a528ac

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

json_duplicate_keys/__init__.py

+60-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import json
2-
31
# # # # # # # # # # # # # # # # # # # # # # #
4-
# # # # # # # # # # Loads # # # # # # # # # #
2+
# # # # # # # # # # loads # # # # # # # # # #
53
# # # # # # # # # # # # # # # # # # # # # # #
64
"""
75
>>> INPUT: '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "22.3.3", "version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
86
97
<<< OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': '22.3.3', 'version{{{_2_}}}': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
108
"""
119
def loads(Jstr, dupSign_start="{", dupSign_end="}", _isDebug_=False):
10+
import json
1211
import re
1312
from collections import OrderedDict
1413
import traceback
@@ -117,14 +116,40 @@ def __convert_Jloads_to_Jobj(Jloads, Jobj):
117116

118117

119118
# # # # # # # # # # # # # # # # # # # # # # #
120-
# # # # # # # # # # Dumps # # # # # # # # # #
119+
# # # # # # # # # # load # # # # # # # # # #
120+
# # # # # # # # # # # # # # # # # # # # # # #
121+
"""
122+
"""
123+
def load(Jfilepath, dupSign_start="{", dupSign_end="}", _isDebug_=False):
124+
import traceback
125+
126+
try:
127+
Jfile = open(Jfilepath)
128+
Jstr = Jfile.read()
129+
Jfile.close()
130+
except:
131+
if _isDebug_: traceback.print_exc()
132+
return None
133+
134+
return loads(Jstr, dupSign_start=dupSign_start, dupSign_end=dupSign_end, _isDebug_=_isDebug_)
135+
# # # # # # # # # # # # # # # # # # # # # # #
136+
# # # # # # # # # # # # # # # # # # # # # # #
137+
# # # # # # # # # # # # # # # # # # # # # # #
138+
139+
140+
141+
142+
143+
# # # # # # # # # # # # # # # # # # # # # # #
144+
# # # # # # # # # # dumps # # # # # # # # # #
121145
# # # # # # # # # # # # # # # # # # # # # # #
122146
"""
123147
>>> INPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': '22.3.14', 'release': [{'version': '22.3.3', 'version{{{_2_}}}': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
124148
125149
<<< OUTPUT: '{"author": "truocphan", "version": "22.3.3", "version": "22.3.14", "release": [{"version": "22.3.3", "version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
126150
"""
127151
def dumps(Jobj, dupSign_start="{", dupSign_end="}", _isDebug_=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
152+
import json
128153
import re
129154
from collections import OrderedDict
130155
import traceback
@@ -165,7 +190,35 @@ def dumps(Jobj, dupSign_start="{", dupSign_end="}", _isDebug_=False, skipkeys=Fa
165190

166191

167192
# # # # # # # # # # # # # # # # # # # # # # #
168-
# # # # # # # # # Flatten # # # # # # # # # #
193+
# # # # # # # # # # dump # # # # # # # # # #
194+
# # # # # # # # # # # # # # # # # # # # # # #
195+
"""
196+
"""
197+
def dump(Jobj, Jfilepath, dupSign_start="{", dupSign_end="}", _isDebug_=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
198+
import traceback
199+
200+
Jstr = dumps(Jobj, dupSign_start=dupSign_start, dupSign_end=dupSign_end, _isDebug_=_isDebug_, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular, allow_nan=allow_nan, cls=cls, indent=indent, separators=separators, default=default, sort_keys=sort_keys)
201+
202+
if Jstr == None:
203+
return False
204+
try:
205+
Jfile = open(Jfilepath, "w")
206+
Jfile.write(Jstr)
207+
Jfile.close()
208+
return True
209+
except:
210+
if _isDebug_: traceback.print_exc()
211+
return False
212+
# # # # # # # # # # # # # # # # # # # # # # #
213+
# # # # # # # # # # # # # # # # # # # # # # #
214+
# # # # # # # # # # # # # # # # # # # # # # #
215+
216+
217+
218+
219+
220+
# # # # # # # # # # # # # # # # # # # # # # #
221+
# # # # # # # # # flatten # # # # # # # # # #
169222
# # # # # # # # # # # # # # # # # # # # # # #
170223
"""
171224
>>> INPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': '22.3.14', 'release': [{'version': '22.3.3', 'version{{{_2_}}}': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
@@ -237,7 +290,7 @@ def __convert_Jobj_to_Jflat(Jobj, key=None):
237290

238291

239292
# # # # # # # # # # # # # # # # # # # # # # #
240-
# # # # # # # # # Unflatten # # # # # # # # #
293+
# # # # # # # # # unflatten # # # # # # # # #
241294
# # # # # # # # # # # # # # # # # # # # # # #
242295
"""
243296
>>> INPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': '22.3.14', 'release||$0$||version': '22.3.3', 'release||$0$||version{{{_2_}}}': 'latest', 'snapshot||author': 'truocphan', 'snapshot||version': '22.3.3', 'snapshot||release||$0$||version': '22.3.14'}
@@ -283,4 +336,4 @@ def unflatten(Jflat, separator="||", parse_index="$", _isDebug_=False):
283336
return None
284337
# # # # # # # # # # # # # # # # # # # # # # #
285338
# # # # # # # # # # # # # # # # # # # # # # #
286-
# # # # # # # # # # # # # # # # # # # # # # #
339+
# # # # # # # # # # # # # # # # # # # # # # #

0 commit comments

Comments
 (0)