4
4
except NameError :
5
5
unicode = str # Python 3
6
6
7
+ from collections import OrderedDict
8
+ import json , re , copy
9
+
7
10
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
8
11
# # # # # # # # # # # Normalize Key name # # # # # # # # # # #
9
12
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
10
13
def normalize_key (name , dupSign_start = "{{{" , dupSign_end = "}}}" , _isDebug_ = False ):
11
- import re
12
-
13
14
# User input data type validation
14
15
if type (_isDebug_ ) != bool : _isDebug_ = False
15
16
@@ -31,9 +32,6 @@ def normalize_key(name, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=False)
31
32
# # # # # # # # # # # # # # # loads # # # # # # # # # # # # # #
32
33
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
33
34
def loads (Jstr , dupSign_start = "{{{" , dupSign_end = "}}}" , ordered_dict = False , _isDebug_ = False ):
34
- import json , re
35
- from collections import OrderedDict
36
-
37
35
# User input data type validation
38
36
if type (_isDebug_ ) != bool : _isDebug_ = False
39
37
@@ -169,7 +167,6 @@ def load(Jfilepath, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False,
169
167
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
170
168
class JSON_DUPLICATE_KEYS :
171
169
def __init__ (self , Jobj ):
172
- from collections import OrderedDict
173
170
self .__Jobj = dict ()
174
171
if type (Jobj ) in [dict , OrderedDict , list ]:
175
172
self .__Jobj = Jobj
@@ -188,9 +185,6 @@ def getObject(self):
188
185
# # # # # # # # # # # # # # # get # # # # # # # # # # # # # # #
189
186
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
190
187
def get (self , name , case_insensitive = False , separator = "||" , parse_index = "$" , _isDebug_ = False ):
191
- import re
192
- from collections import OrderedDict
193
-
194
188
# User input data type validation
195
189
if type (_isDebug_ ) != bool : _isDebug_ = False
196
190
@@ -247,9 +241,6 @@ def get(self, name, case_insensitive=False, separator="||", parse_index="$", _is
247
241
# # # # # # # # # # # # # # # set # # # # # # # # # # # # # # #
248
242
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
249
243
def set (self , name , value , case_insensitive = False , separator = "||" , parse_index = "$" , dupSign_start = "{{{" , dupSign_end = "}}}" , ordered_dict = False , _isDebug_ = False ):
250
- import re
251
- from collections import OrderedDict
252
-
253
244
# User input data type validation
254
245
if type (_isDebug_ ) != bool : _isDebug_ = False
255
246
@@ -329,12 +320,55 @@ def set(self, name, value, case_insensitive=False, separator="||", parse_index="
329
320
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
330
321
331
322
323
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
324
+ # # # # # # # # # # # # # # insert # # # # # # # # # # # # # #
325
+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
326
+ def insert (self , name , value , position = None , case_insensitive = False , separator = "||" , parse_index = "$" , dupSign_start = "{{{" , dupSign_end = "}}}" , _isDebug_ = False ):
327
+ # User input data type validation
328
+ if type (_isDebug_ ) != bool : _isDebug_ = False
329
+
330
+ if type (name ) not in [str , unicode ]:
331
+ if _isDebug_ : print ("\x1b [31m[-] DataTypeError: the KEY name must be str or unicode, not {}\x1b [0m" .format (type (name )))
332
+ return False
333
+
334
+ if type (position ) != int : position = None
335
+
336
+ if type (case_insensitive ) != bool : case_insensitive = False
337
+
338
+ if type (separator ) not in [str , unicode ]: separator = "||"
339
+
340
+ if type (parse_index ) not in [str , unicode ]: parse_index = "$"
341
+
342
+ if type (dupSign_start ) not in [str , unicode ]: dupSign_start = "{{{"
343
+
344
+ if type (dupSign_end ) not in [str , unicode ]: dupSign_end = "}}}"
345
+
346
+ if type (self .getObject ()) not in [list , dict , OrderedDict ]:
347
+ if _isDebug_ : print ("\x1b [31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b [0m" .format (type (self .getObject ())))
348
+ return False
349
+
350
+ if re .search (re .escape (separator )+ "$" , name ):
351
+ if _isDebug_ : print ("\x1b [31m[-] KeyNameInvalidError: \x1b [0m" + name )
352
+ return False
353
+
354
+ Jget = self .get (name , case_insensitive = case_insensitive , separator = separator , parse_index = parse_index , _isDebug_ = _isDebug_ )
355
+
356
+ if Jget ["value" ] != "JSON_DUPLICATE_KEYS_ERROR" :
357
+ if type (Jget ["value" ]) == list :
358
+ if position == None : position = len (Jget ["value" ])
359
+
360
+ Jget ["value" ].insert (position , value )
361
+
362
+ return self .update (Jget ["name" ], Jget ["value" ], separator = separator , parse_index = parse_index , dupSign_start = dupSign_start , dupSign_end = dupSign_end , _isDebug_ = _isDebug_ )
363
+ else :
364
+ if _isDebug_ : print ("\x1b [31m[-] DataTypeError: The data type of {} must be list, not {}\x1b [0m" .format (Jget ["name" ], type (Jget ["value" ])))
365
+ return False
366
+
367
+
332
368
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
333
369
# # # # # # # # # # # # # # update # # # # # # # # # # # # # #
334
370
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
335
371
def update (self , name , value , case_insensitive = False , allow_new_key = False , separator = "||" , parse_index = "$" , dupSign_start = "{{{" , dupSign_end = "}}}" , ordered_dict = False , _isDebug_ = False ):
336
- import re
337
-
338
372
# User input data type validation
339
373
if type (_isDebug_ ) != bool : _isDebug_ = False
340
374
@@ -379,8 +413,6 @@ def update(self, name, value, case_insensitive=False, allow_new_key=False, separ
379
413
# # # # # # # # # # # # # # delete # # # # # # # # # # # # #
380
414
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
381
415
def delete (self , name , case_insensitive = False , separator = "||" , parse_index = "$" , _isDebug_ = False ):
382
- import re
383
-
384
416
# User input data type validation
385
417
if type (_isDebug_ ) != bool : _isDebug_ = False
386
418
@@ -420,8 +452,6 @@ def delete(self, name, case_insensitive=False, separator="||", parse_index="$",
420
452
# # # # # # # # # # # # filter_keys # # # # # # # # # # # # #
421
453
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
422
454
def filter_keys (self , name , separator = "||" , parse_index = "$" , ordered_dict = False ):
423
- import re , copy
424
-
425
455
JDKSObject = copy .deepcopy (self )
426
456
JDKSObject .flatten (separator = separator , parse_index = parse_index , ordered_dict = ordered_dict )
427
457
newJDKSObject = loads ("{}" , ordered_dict = ordered_dict )
@@ -444,8 +474,6 @@ def filter_keys(self, name, separator="||", parse_index="$", ordered_dict=False)
444
474
# # # # # # # # # # # # filter_values # # # # # # # # # # # # #
445
475
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
446
476
def filter_values (self , value , separator = "||" , parse_index = "$" , ordered_dict = False ):
447
- import re , copy
448
-
449
477
JDKSObject = copy .deepcopy (self )
450
478
JDKSObject .flatten (separator = separator , parse_index = parse_index , ordered_dict = ordered_dict )
451
479
newJDKSObject = loads ("{}" , ordered_dict = ordered_dict )
@@ -468,9 +496,6 @@ def filter_values(self, value, separator="||", parse_index="$", ordered_dict=Fal
468
496
# # # # # # # # # # # # # # dumps # # # # # # # # # # # # # #
469
497
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
470
498
def dumps (self , 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 ):
471
- import json , re
472
- from collections import OrderedDict
473
-
474
499
# User input data type validation
475
500
if type (_isDebug_ ) != bool : _isDebug_ = False
476
501
@@ -514,8 +539,6 @@ def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=Fals
514
539
# # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
515
540
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
516
541
def flatten (self , separator = "||" , parse_index = "$" , ordered_dict = False , _isDebug_ = False ):
517
- from collections import OrderedDict
518
-
519
542
# User input data type validation
520
543
if type (_isDebug_ ) != bool : _isDebug_ = False
521
544
@@ -575,9 +598,6 @@ def __convert_Jobj_to_Jflat(Jobj, key=None):
575
598
# # # # # # # # # # # # # unflatten # # # # # # # # # # # # #
576
599
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
577
600
def unflatten (self , separator = "||" , parse_index = "$" , ordered_dict = False , _isDebug_ = False ):
578
- import re
579
- from collections import OrderedDict
580
-
581
601
# User input data type validation
582
602
if type (_isDebug_ ) != bool : _isDebug_ = False
583
603
0 commit comments