@@ -325,64 +325,70 @@ def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True
325325 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
326326
327327
328- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329- # # # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
330- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331- # def flatten(self, separator="||", parse_index="$", ordered_dict=False):
332- # from collections import OrderedDict
328+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329+ # # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
330+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331+ def flatten (self , separator = "||" , parse_index = "$" , ordered_dict = False , _isDebug_ = True ):
332+ from collections import OrderedDict
333333
334- # if len(self.__Jobj) > 0:
335- # Jflat = dict()
336- # if ordered_dict:
337- # Jflat = OrderedDict()
338-
339- # def __convert_Jobj_to_Jflat(Jobj, key=None):
340- # if type(Jobj) in [dict, OrderedDict]:
341- # if len(Jobj) == 0:
342- # Jflat[key] = dict()
343- # if ordered_dict:
344- # Jflat[key] = OrderedDict()
345- # else:
346- # for k,v in Jobj.items():
347- # _Jobj = v
348- # _key = "{key}{separator}{k}".format(key=key,separator=separator,k=k) if key != None else "{k}".format(k=k)
349-
350- # __convert_Jobj_to_Jflat(_Jobj, _key)
351- # elif type(Jobj) == list:
352- # if len(Jobj) == 0:
353- # Jflat[key] = list()
354- # else:
355- # for i,v in enumerate(Jobj):
356- # _Jobj = v
357- # _key = "{key}{separator}{parse_index}{i}{parse_index}".format(key=key, separator=separator, parse_index=parse_index, i=i) if key != None else "{parse_index}{i}{parse_index}".format(parse_index=parse_index, i=i)
334+ if type (self .getObject ()) in [list , dict , OrderedDict ]:
335+ if len (self .getObject ()) > 0 :
336+ try :
337+ Jflat = dict ()
338+ if ordered_dict :
339+ Jflat = OrderedDict ()
340+
341+ def __convert_Jobj_to_Jflat (Jobj , key = None ):
342+ if type (Jobj ) in [dict , OrderedDict ]:
343+ if len (Jobj ) == 0 :
344+ Jflat [key ] = dict ()
345+ if ordered_dict :
346+ Jflat [key ] = OrderedDict ()
347+ else :
348+ for k ,v in Jobj .items ():
349+ _Jobj = v
350+ _key = "{key}{separator}{k}" .format (key = key ,separator = separator ,k = k ) if key != None else "{k}" .format (k = k )
351+
352+ __convert_Jobj_to_Jflat (_Jobj , _key )
353+ elif type (Jobj ) == list :
354+ if len (Jobj ) == 0 :
355+ Jflat [key ] = list ()
356+ else :
357+ for i ,v in enumerate (Jobj ):
358+ _Jobj = v
359+ _key = "{key}{separator}{parse_index}{i}{parse_index}" .format (key = key , separator = separator , parse_index = parse_index , i = i ) if key != None else "{parse_index}{i}{parse_index}" .format (parse_index = parse_index , i = i )
358360
359- # __convert_Jobj_to_Jflat(_Jobj, _key)
360- # else:
361- # Jflat[key] = Jobj
361+ __convert_Jobj_to_Jflat (_Jobj , _key )
362+ else :
363+ Jflat [key ] = Jobj
362364
363365
364- # try:
365- # if type(separator) not in [str, unicode] or len(separator) == 0: separator = "||"
366- # except Exception as e:
367- # if type(separator) != str or len(separator) == 0: separator = "||"
366+ try :
367+ if type (separator ) not in [str , unicode ] or len (separator ) == 0 : separator = "||"
368+ except Exception as e :
369+ if type (separator ) != str or len (separator ) == 0 : separator = "||"
368370
369- # try:
370- # if type(parse_index) not in [str, unicode] or len(parse_index) == 0: parse_index = "$"
371- # except Exception as e:
372- # if type(parse_index) != str or len(parse_index) == 0: parse_index = "$"
371+ try :
372+ if type (parse_index ) not in [str , unicode ] or len (parse_index ) == 0 : parse_index = "$"
373+ except Exception as e :
374+ if type (parse_index ) != str or len (parse_index ) == 0 : parse_index = "$"
373375
374- # __convert_Jobj_to_Jflat(self.__Jobj )
376+ __convert_Jobj_to_Jflat (self .getObject () )
375377
376- # self.__Jobj = Jflat
377- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
378- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
379- # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
378+ self .__Jobj = Jflat
379+ except Exception as e :
380+ if _isDebug_ : print ("\x1b [31m[-] ExceptionError: {}\x1b [0m" .format (e ))
381+ else :
382+ if _isDebug_ : print ("\x1b [31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b [0m" .format (type (self .getObject ())))
383+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
384+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
385+ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
380386
381387
382388 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
383389 # # # # # # # # # # # # # # unflatten # # # # # # # # # # # # #
384390 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
385- # def unflatten(self, separator="||", parse_index="$", ordered_dict=False):
391+ # def unflatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=True ):
386392 # import re
387393 # from collections import OrderedDict
388394
0 commit comments