File tree Expand file tree Collapse file tree
remove-json-keys/src/remove_json_keys/lib/data
translate-messages/src/translate_messages/lib/data Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55
66from . import file
77
8+ def is_valid (file_path , format = 'json' ):
9+ file_path = Path (file_path )
10+ if not file_path .exists ():
11+ return False
12+ try : file_text = file_path .read_text (encoding = 'utf-8' )
13+ except UnicodeDecodeError :
14+ return False
15+ if format == 'json' :
16+ try : json .loads (file_text ) ; return True
17+ except Exception : return False
18+ elif format == 'json5' :
19+ try : json5 .loads (file_text ) ; return True
20+ except Exception : return False
21+ else :
22+ raise ValueError (f"Unsupported format '{ format } '. Expected 'json' or 'json5'." )
23+
824def read (file_path , encoding = 'utf-8' ):
925 with open (file_path , 'r' , encoding = encoding ) as file :
1026 return json5 .load (file )
Original file line number Diff line number Diff line change 33
44import json5
55
6+ def is_valid (file_path , format = 'json' ):
7+ file_path = Path (file_path )
8+ if not file_path .exists ():
9+ return False
10+ try : file_text = file_path .read_text (encoding = 'utf-8' )
11+ except UnicodeDecodeError :
12+ return False
13+ if format == 'json' :
14+ try : json .loads (file_text ) ; return True
15+ except Exception : return False
16+ elif format == 'json5' :
17+ try : json5 .loads (file_text ) ; return True
18+ except Exception : return False
19+ else :
20+ raise ValueError (f"Unsupported format '{ format } '. Expected 'json' or 'json5'." )
21+
622def read (file_path , encoding = 'utf-8' ):
723 with open (file_path , 'r' , encoding = encoding ) as file :
824 return json5 .load (file )
You can’t perform that action at this time.
0 commit comments