@@ -5,6 +5,12 @@ def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isD
5
5
import json , re
6
6
from collections import OrderedDict
7
7
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
+
8
14
if type (Jstr ) in [str ]:
9
15
def __convert_Jloads_to_Jobj (Jloads , Jobj ):
10
16
if type (Jloads ) in [dict , OrderedDict ]:
@@ -51,21 +57,6 @@ def __convert_Jloads_to_Jobj(Jloads, Jobj):
51
57
__convert_Jloads_to_Jobj (Jloads [i ], Jobj [i ])
52
58
53
59
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
-
69
60
Jloads = json .loads (Jstr )
70
61
if ordered_dict :
71
62
Jloads = json .loads (Jstr , object_pairs_hook = OrderedDict )
@@ -297,15 +288,15 @@ def set(self, name, value, separator="||", parse_index="$", dupSign_start="{{{",
297
288
298
289
exec (exec_expression + "[" + repr (name_split_lastKey )+ "]=" + repr (value ))
299
290
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 )))
301
292
# Add new key
302
293
elif len (name_split_first ) == 0 :
303
294
if type (self .getObject ()) == list :
304
295
if name_split_lastKey == "" :
305
296
self .__Jobj .append (value )
306
297
else :
307
298
self .__Jobj .append ({name_split_lastKey : value })
308
- elif type ( self . getObject ()) == dict :
299
+ else :
309
300
self .__Jobj [name_split_lastKey ] = value
310
301
else :
311
302
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
387
378
import json , re
388
379
from collections import OrderedDict
389
380
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
395
385
386
+ if type (self .getObject ()) in [list , dict , OrderedDict ]:
396
387
dupSign_start_escape_regex = re .escape (json .dumps ({dupSign_start :"" })[2 :- 6 ])
397
388
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
-
404
389
dupSign_end_escape_regex = re .escape (json .dumps ({dupSign_end :"" })[2 :- 6 ])
405
390
406
-
407
391
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 ))
408
392
else :
409
393
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
492
476
import re
493
477
from collections import OrderedDict
494
478
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
+
495
485
if type (self .getObject ()) in [dict , OrderedDict ]:
496
486
if len (self .getObject ()) > 0 :
497
487
try :
0 commit comments