Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 95d5104

Browse files
committed
Added new functions: JSON_DUPLICATE_KEYS.dumps(), JSON_DUPLICATE_KEYS.dump()
1 parent 8641a10 commit 95d5104

File tree

2 files changed

+89
-39
lines changed

2 files changed

+89
-39
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,55 @@ print(JDKSObject.getObject())
177177

178178
### JSON_DUPLICATE_KEYS.dumps(`dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=True, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
179179
_Serialize a JSON object to a JSON format string_
180+
- `dupSign_start`:
181+
- `dupSign_end`:
182+
- `_isDebug_`: Show/ Hide debug error messages
183+
- For remaining arguments, please refer to [json.dump()](https://docs.python.org/3/library/json.html#json.dump)
180184
```python
185+
import json_duplicate_keys as jdks
186+
187+
Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
188+
189+
JDKSObject = jdks.loads(Jstr)
190+
191+
print(JDKSObject.getObject())
192+
# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
193+
194+
JDKSObject.delete("version")
195+
JDKSObject.delete("release||$0$")
196+
JDKSObject.delete("snapshot")
197+
198+
print(JDKSObject.dumps())
199+
# OUTPUT: {"author": "truocphan", "version": "latest", "release": []}
181200
```
182201

183202
### JSON_DUPLICATE_KEYS.dump(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=True, `skipkeys`=False, `ensure_ascii`=True, `check_circular`=True, `allow_nan`=True, `cls`=None, `indent`=None, `separators`=None, `default`=None, `sort_keys`=False)
184203
_Serialize a JSON object to a JSON format string and write to a file_
204+
- `Jfilepath`: the path to the file to save the JSON format string
205+
- `dupSign_start`:
206+
- `dupSign_end`:
207+
- `_isDebug_`: Show/ Hide debug error messages
208+
- For remaining arguments, please refer to [json.dump()](https://docs.python.org/3/library/json.html#json.dump)
185209
```python
210+
import json_duplicate_keys as jdks
211+
212+
Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
213+
214+
JDKSObject = jdks.loads(Jstr)
215+
216+
print(JDKSObject.getObject())
217+
# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
218+
219+
JDKSObject.delete("version")
220+
JDKSObject.delete("release||$0$")
221+
JDKSObject.delete("snapshot")
222+
223+
Jfilepath = "/path/to/file.json"
224+
JDKSObject.dump(Jfilepath)
225+
226+
JDKSObject_load = jdks.load(Jfilepath)
227+
print(JDKSObject_load.getObject())
228+
# OUTPUT: {'author': 'truocphan', 'version': 'latest', 'release': []}
186229
```
187230

188231
### JSON_DUPLICATE_KEYS.flatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=True)

json_duplicate_keys/__init__.py

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
167167
if type(self.getObject()) in [list, dict, OrderedDict]:
168168
try:
169169
Jobj = self.__Jobj
170-
Jval = "JSON_DUPLICATE_KEYS_NOT_FOUND"
170+
Jval = "JSON_DUPLICATE_KEYS_ERROR"
171171
name_split = name.split(separator)
172172

173173
for i in range(len(name_split)):
@@ -179,14 +179,14 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
179179
Jobj = Jobj[int(name_split[i].split(parse_index)[1])]
180180
else:
181181
if _isDebug_: print("\x1b[31m[-] KeyNotFoundError: \x1b[0m"+separator.join(name_split[:i+1]))
182-
return "JSON_DUPLICATE_KEYS_NOT_FOUND"
182+
return "JSON_DUPLICATE_KEYS_ERROR"
183183
return Jval
184184
except Exception as e:
185185
if _isDebug_: print("\x1b[31m[-] ExceptionError: {}\x1b[0m".format(e))
186-
return "JSON_DUPLICATE_KEYS_NOT_FOUND"
186+
return "JSON_DUPLICATE_KEYS_ERROR"
187187
else:
188188
if _isDebug_: print("\x1b[31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b[0m".format(type(self.getObject())))
189-
return "JSON_DUPLICATE_KEYS_NOT_FOUND"
189+
return "JSON_DUPLICATE_KEYS_ERROR"
190190
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
191191
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
192192
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -206,7 +206,7 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
206206
# name_split_current = name_split[i-1]
207207
# name_split_last = separator.join(name_split[i:])
208208

209-
# if self.get(separator.join([name_split_first, name_split_current]), separator=separator, parse_index=parse_index, _isDebug_=False) != "JSON_DUPLICATE_KEYS_NOT_FOUND":
209+
# if self.get(separator.join([name_split_first, name_split_current]), separator=separator, parse_index=parse_index, _isDebug_=False) != "JSON_DUPLICATE_KEYS_ERROR":
210210
# if re.search("^"+re.escape(parse_index)+"\d+"+re.escape(parse_index)+"$", name_split_current):
211211
# k = separator.join([ name_split_first, parse_index+str(len(self.get(name_split_first, separator=separator, parse_index=parse_index, _isDebug_=False)))+parse_index, separator.join([re.sub("^"+re.escape(parse_index)+"\d+"+re.escape(parse_index)+"$",parse_index+"0"+parse_index, ns) for ns in name_split_last.split(separator)]) ])
212212
# self.flatten()
@@ -215,7 +215,7 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
215215
# else:
216216
# i = 2
217217
# while True:
218-
# if self.get(separator.join(name_split_first, name_split_current)+dupSign_start+"_"+str(i)+"_"+dupSign_end, separator=separator, parse_index=parse_index, _isDebug_=False) == "JSON_DUPLICATE_KEYS_NOT_FOUND":
218+
# if self.get(separator.join(name_split_first, name_split_current)+dupSign_start+"_"+str(i)+"_"+dupSign_end, separator=separator, parse_index=parse_index, _isDebug_=False) == "JSON_DUPLICATE_KEYS_ERROR":
219219
# self.flatten()
220220
# self.__Jobj[separator.join([separator.join(name_split_first, name_split_current)+dupSign_start+"_"+str(i)+"_"+dupSign_end]), separator.join([re.sub("^"+re.escape(parse_index)+"\d+"+re.escape(parse_index)+"$",parse_index+"0"+parse_index, ns) for ns in name_split_last.split(separator)])] = value
221221
# self.unflatten()
@@ -233,7 +233,7 @@ def get(self, name, separator="||", parse_index="$", _isDebug_=True):
233233
def update(self, name, value, separator="||", parse_index="$", _isDebug_=True):
234234
import re
235235

236-
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_NOT_FOUND":
236+
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_ERROR":
237237
try:
238238
exec_expression = "self.getObject()"
239239

@@ -257,7 +257,7 @@ def update(self, name, value, separator="||", parse_index="$", _isDebug_=True):
257257
def delete(self, name, separator="||", parse_index="$", _isDebug_=True):
258258
import re
259259

260-
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_NOT_FOUND":
260+
if self.get(name, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_) != "JSON_DUPLICATE_KEYS_ERROR":
261261
try:
262262
exec_expression = "del self.getObject()"
263263

@@ -275,47 +275,54 @@ def delete(self, name, separator="||", parse_index="$", _isDebug_=True):
275275
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
276276

277277

278-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
279-
# # # # # # # # # # # # # # # dumps # # # # # # # # # # # # # #
280-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
281-
# def dumps(self, dupSign_start="{{{", dupSign_end="}}}", skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
282-
# import json, re
283-
# from collections import OrderedDict
278+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
279+
# # # # # # # # # # # # # # dumps # # # # # # # # # # # # # #
280+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
281+
def dumps(self, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
282+
import json, re
283+
from collections import OrderedDict
284284

285-
# try:
286-
# if type(dupSign_start) not in [str, unicode]: dupSign_start = "{{{"
287-
# except Exception as e:
288-
# if type(dupSign_start) != str: dupSign_start = "{{{"
285+
if type(self.getObject()) in [list, dict, OrderedDict]:
286+
try:
287+
if type(dupSign_start) not in [str, unicode]: dupSign_start = "{{{"
288+
except Exception as e:
289+
if type(dupSign_start) != str: dupSign_start = "{{{"
289290

290-
# dupSign_start_escape_regex = re.escape(json.dumps({dupSign_start:""})[2:-6])
291+
dupSign_start_escape_regex = re.escape(json.dumps({dupSign_start:""})[2:-6])
291292

292293

293-
# try:
294-
# if type(dupSign_end) not in [str, unicode]: dupSign_end = "}}}"
295-
# except Exception as e:
296-
# if type(dupSign_end) != str: dupSign_end = "}}}"
294+
try:
295+
if type(dupSign_end) not in [str, unicode]: dupSign_end = "}}}"
296+
except Exception as e:
297+
if type(dupSign_end) != str: dupSign_end = "}}}"
297298

298-
# dupSign_end_escape_regex = re.escape(json.dumps({dupSign_end:""})[2:-6])
299+
dupSign_end_escape_regex = re.escape(json.dumps({dupSign_end:""})[2:-6])
299300

300301

301-
# return re.sub(r'{dupSign_start}_\d+_{dupSign_end}":'.format(dupSign_start=dupSign_start_escape_regex, dupSign_end=dupSign_end_escape_regex), '":', json.dumps(self.__Jobj, 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))
302-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
303-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
304-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
302+
return re.sub(r'{dupSign_start}_\d+_{dupSign_end}":'.format(dupSign_start=dupSign_start_escape_regex, dupSign_end=dupSign_end_escape_regex), '":', json.dumps(self.getObject(), 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))
303+
else:
304+
if _isDebug_: print("\x1b[31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b[0m".format(type(self.getObject())))
305+
return "JSON_DUPLICATE_KEYS_ERROR"
306+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
307+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
308+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
305309

306310

307-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
308-
# # # # # # # # # # # # # # # dump # # # # # # # # # # # # # #
309-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
310-
# def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
311-
# Jstr = self.dumps(dupSign_start=dupSign_start, dupSign_end=dupSign_end, 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)
311+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
312+
# # # # # # # # # # # # # # dump # # # # # # # # # # # # # #
313+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
314+
def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False):
315+
Jstr = self.dumps(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)
312316

313-
# Jfile = open(Jfilepath, "w")
314-
# Jfile.write(Jstr)
315-
# Jfile.close()
316-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
317-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
318-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
317+
try:
318+
Jfile = open(Jfilepath, "w")
319+
Jfile.write(Jstr)
320+
Jfile.close()
321+
except Exception as e:
322+
if _isDebug_: print("\x1b[31m[-] ExceptionError: {}\x1b[0m".format(e))
323+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
324+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
325+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
319326

320327

321328
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

0 commit comments

Comments
 (0)