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

Commit 62f7e0e

Browse files
committed
Updated json_duplicate_keys v2023.8.8
1 parent 2312eca commit 62f7e0e

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "relea
3434
JDKSObject = jdks.loads(Jstr)
3535

3636
print(JDKSObject)
37-
3837
# OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE987940>
3938
```
4039
---
@@ -56,7 +55,6 @@ Jfilepath = "/path/to/file.json"
5655
JDKSObject = jdks.load(Jfilepath)
5756

5857
print(JDKSObject)
59-
6058
# OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE986D40>
6159
```
6260
---
@@ -71,7 +69,6 @@ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "relea
7169
JDKSObject = jdks.loads(Jstr)
7270

7371
print(JDKSObject.getObject())
74-
7572
# 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'}]}}
7673
```
7774
---

json_duplicate_keys/__init__.py

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isD
55
import json, re
66
from collections import OrderedDict
77

8+
# User input data type validation
9+
if type(dupSign_start) != str: dupSign_start = "{{{"
10+
if type(dupSign_end) != str: dupSign_end = "}}}"
11+
if type(ordered_dict) != bool: ordered_dict = False
12+
if type(_isDebug_) != bool: _isDebug_ = True
13+
814
if type(Jstr) in [str]:
915
def __convert_Jloads_to_Jobj(Jloads, Jobj):
1016
if type(Jloads) in [dict, OrderedDict]:
@@ -51,21 +57,6 @@ def __convert_Jloads_to_Jobj(Jloads, Jobj):
5157
__convert_Jloads_to_Jobj(Jloads[i], Jobj[i])
5258

5359
try:
54-
# User input data type validation
55-
try:
56-
if type(dupSign_start) not in [str, unicode] or len(dupSign_start) == 0: dupSign_start = "{{{"
57-
except Exception as e:
58-
if type(dupSign_start) != str or len(dupSign_start) == 0: dupSign_start = "{{{"
59-
60-
try:
61-
if type(dupSign_end) not in [str, unicode] or len(dupSign_end) == 0: dupSign_end = "}}}"
62-
except Exception as e:
63-
if type(dupSign_end) != str or len(dupSign_end) == 0: dupSign_end = "}}}"
64-
65-
if type(ordered_dict) != bool: ordered_dict = False
66-
if type(_isDebug_) != bool: _isDebug_ = True
67-
68-
6960
Jloads = json.loads(Jstr)
7061
if ordered_dict:
7162
Jloads = json.loads(Jstr, object_pairs_hook=OrderedDict)
@@ -297,15 +288,15 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
297288

298289
exec(exec_expression+"["+repr(name_split_lastKey)+"]="+repr(value))
299290
else:
300-
if _isDebug_: print("\x1b[31m[-] KeyNameInvalidError: {}\x1b[0m".format(separator.join(name_split_first)))
291+
if _isDebug_: print("\x1b[31m[-] KeyNameNotExistError: {}\x1b[0m".format(separator.join(name_split_first)))
301292
# Add new key
302293
elif len(name_split_first) == 0:
303294
if type(self.getObject()) == list:
304295
if name_split_lastKey == "":
305296
self.__Jobj.append(value)
306297
else:
307298
self.__Jobj.append({name_split_lastKey: value})
308-
elif type(self.getObject()) == dict:
299+
else:
309300
self.__Jobj[name_split_lastKey] = value
310301
else:
311302
if _isDebug_: print("\x1b[31m[-] KeyNameInvalidError: {}\x1b[0m".format(separator.join(name_split_first)))
@@ -387,23 +378,16 @@ def dumps(self, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True, skipkeys
387378
import json, re
388379
from collections import OrderedDict
389380

390-
if type(self.getObject()) in [list, dict, OrderedDict]:
391-
try:
392-
if type(dupSign_start) not in [str, unicode]: dupSign_start = "{{{"
393-
except Exception as e:
394-
if type(dupSign_start) != str: dupSign_start = "{{{"
381+
# User input data type validation
382+
if type(dupSign_start) != str: dupSign_start = "{{{"
383+
if type(dupSign_end) != str: dupSign_end = "}}}"
384+
if type(_isDebug_) != bool: _isDebug_ = True
395385

386+
if type(self.getObject()) in [list, dict, OrderedDict]:
396387
dupSign_start_escape_regex = re.escape(json.dumps({dupSign_start:""})[2:-6])
397388

398-
399-
try:
400-
if type(dupSign_end) not in [str, unicode]: dupSign_end = "}}}"
401-
except Exception as e:
402-
if type(dupSign_end) != str: dupSign_end = "}}}"
403-
404389
dupSign_end_escape_regex = re.escape(json.dumps({dupSign_end:""})[2:-6])
405390

406-
407391
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))
408392
else:
409393
if _isDebug_: print("\x1b[31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b[0m".format(type(self.getObject())))
@@ -492,6 +476,12 @@ def unflatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebu
492476
import re
493477
from collections import OrderedDict
494478

479+
# User input data type validation
480+
if type(separator) != str: separator = "||"
481+
if type(parse_index) != str: parse_index = "$"
482+
if type(ordered_dict) != bool: ordered_dict = False
483+
if type(_isDebug_) != bool: _isDebug_ = True
484+
495485
if type(self.getObject()) in [dict, OrderedDict]:
496486
if len(self.getObject()) > 0:
497487
try:

0 commit comments

Comments
 (0)