def parse_json_file(conf, file_node): # Do not follow python json implemented RFC 7159 standard. # Duplicate keys are not allowed. def dupe_checking_hook(pairs): result = OrderedDict() for key, val in pairs: key = _decode_unicode_to_utf8(key) val = _decode_unicode_to_utf8(val) if key in result: raise KeyError("Duplicate key specified: %s" % key) result[key] = val return result try: file_content_raw = file_node.read() file_content_parsed = json.loads(file_content_raw, object_hook=_decode_unicode_to_utf8, object_pairs_hook=dupe_checking_hook) return file_content_parsed except Exception as e: line_num = 0 exception_str = str(e) # Handle invalid last entry in list error if "No JSON object could be decoded" in exception_str: cur_line = "" prev_line = "" file_content_by_line = file_content_raw.split('\n') for lineIndex, line in enumerate(file_content_by_line): # Sanitize string cur_line = ''.join(line.split()) # Handle empty line if not cur_line: continue # Check for invalid JSON schema if any(substring in (prev_line + cur_line) for substring in [",]", ",}"]): line_num = lineIndex exception_str = 'Invalid JSON, last list/dictionary entry should not end with a ",". [Original exception: "%s"]' % exception_str break prev_line = cur_line # If exception has not been handled yet if not line_num: # Search for 'line' in exception and output pure string exception_str_list = exception_str.split(" ") for index, elem in enumerate(exception_str_list): if elem == 'line': line_num = exception_str_list[index + 1] break # Raise fatal error conf.cry_file_error(exception_str, file_node.abspath(), line_num)
def save_to_json_file(conf, file_node, data): try: #data_json = json.dumps(data) #Logs.info('JSON DATA: %s' % data_json) file_node.write(json.dumps(data)) except Exception as e: line_num = 0 exception_str = str(e) conf.cry_file_error(exception_str, file_node.abspath(), 0)
def parse_json_file(conf, file_node): # Do not follow python json implemented RFC 7159 standard. # Duplicate keys are not allowed. def dupe_checking_hook(pairs): result = OrderedDict() for key,val in pairs: key = _decode_unicode_to_utf8(key) val = _decode_unicode_to_utf8(val) if key in result: raise KeyError("Duplicate key specified: %s" % key) result[key] = val return result try: file_content_raw = file_node.read() file_content_parsed = json.loads(file_content_raw, object_hook=_decode_unicode_to_utf8, object_pairs_hook = dupe_checking_hook ) return file_content_parsed except Exception as e: line_num = 0 exception_str = str(e) # Handle invalid last entry in list error if "No JSON object could be decoded" in exception_str: cur_line = "" prev_line = "" file_content_by_line = file_content_raw.split('\n') for lineIndex, line in enumerate(file_content_by_line): # Sanitize string cur_line = ''.join(line.split()) # Handle empty line if not cur_line: continue # Check for invalid JSON schema if any(substring in (prev_line + cur_line) for substring in [",]", ",}"]): line_num = lineIndex exception_str = 'Invalid JSON, last list/dictionary entry should not end with a ",". [Original exception: "%s"]' % exception_str break; prev_line = cur_line # If exception has not been handled yet if not line_num: # Search for 'line' in exception and output pure string exception_str_list = exception_str.split(" ") for index, elem in enumerate(exception_str_list): if elem == 'line': line_num = exception_str_list[index+1] break # Raise fatal error conf.cry_file_error(exception_str, file_node.abspath(), line_num)
def parse_json_file(conf, file_node): try: file_content_raw = file_node.read() file_content_parsed = json.loads(file_content_raw, object_hook=_decode_dict) return file_content_parsed except Exception as e: line_num = 0 exception_str = str(e) # Handle invalid last entry in list error if "No JSON object could be decoded" in exception_str: cur_line = "" prev_line = "" file_content_by_line = file_content_raw.split('\n') for lineIndex, line in enumerate(file_content_by_line): # Sanitize string cur_line = ''.join(line.split()) # Handle empty line if not cur_line: continue # Check for invalid JSON schema if any(substring in (prev_line + cur_line) for substring in [",]", ",}"]): line_num = lineIndex exception_str = 'Invalid JSON, last list/dictionary entry should not end with a ",". [Original exception: "%s"]' % exception_str break prev_line = cur_line # If exception has not been handled yet if not line_num: # Search for 'line' in exception and output pure string exception_str_list = exception_str.split(" ") for index, elem in enumerate(exception_str_list): if elem == 'line': line_num = exception_str_list[index + 1] break # Raise fatal error conf.cry_file_error(exception_str, file_node.abspath(), line_num)