cornsnake.util_json
Functions for reading from and writing to a JSON file. The read_from_json_file
function reads JSON data from a file, and the write_to_json_file
function writes JSON data to a file.
1""" 2Functions for reading from and writing to a JSON file. The `read_from_json_file` function reads JSON data from a file, and the `write_to_json_file` function writes JSON data to a file. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_json.html) 5""" 6 7import json 8import typing 9 10 11def _json_to_string(value_json: typing.Any) -> str: 12 return json.dumps(value_json, indent=0) 13 14 15def are_same(settings1_json_str: str, settings2_json_str: str) -> bool: 16 """Function to compare two JSON objects, ignoring differences in whitespace.""" 17 18 # parse and serialize to ignore any formatting differences: 19 def _parse_and_serialize(json_str: str) -> str: 20 value_json = json.loads(json_str) 21 return _json_to_string(value_json) 22 23 return _parse_and_serialize(settings1_json_str) == _parse_and_serialize( 24 settings2_json_str 25 ) 26 27 28def read_from_json_file(path_to_json: str, encoding: str = "utf-8") -> typing.Any: 29 """ 30 Function to read JSON data from a file. 31 32 Args: 33 path_to_json (str): The path to the JSON file. 34 encoding (str): The encoding of the file. Default is 'utf-8'. 35 36 Returns: 37 dict: The JSON data read from the file. 38 """ 39 with open(path_to_json, encoding=encoding) as f: 40 data = json.load(f) 41 return data 42 43 44def write_to_json_file( 45 dict: dict, file_path: str, encoding: str = "utf-8", indent: int = 2 46) -> None: 47 """ 48 Function to write JSON data to a file. 49 50 Args: 51 dict (dict): The dictionary to be written to the file as JSON. 52 file_path (str): The path to the output JSON file. 53 encoding (str): The encoding of the file. Default is 'utf-8'. 54 indent (int): The number of spaces to use for indentation. Default is 2. 55 """ 56 json_object = json.dumps(dict, indent=indent) 57 58 with open(file_path, "w", encoding=encoding) as outfile: 59 outfile.write(json_object)
def
are_same(settings1_json_str: str, settings2_json_str: str) -> bool:
16def are_same(settings1_json_str: str, settings2_json_str: str) -> bool: 17 """Function to compare two JSON objects, ignoring differences in whitespace.""" 18 19 # parse and serialize to ignore any formatting differences: 20 def _parse_and_serialize(json_str: str) -> str: 21 value_json = json.loads(json_str) 22 return _json_to_string(value_json) 23 24 return _parse_and_serialize(settings1_json_str) == _parse_and_serialize( 25 settings2_json_str 26 )
Function to compare two JSON objects, ignoring differences in whitespace.
def
read_from_json_file(path_to_json: str, encoding: str = 'utf-8') -> Any:
29def read_from_json_file(path_to_json: str, encoding: str = "utf-8") -> typing.Any: 30 """ 31 Function to read JSON data from a file. 32 33 Args: 34 path_to_json (str): The path to the JSON file. 35 encoding (str): The encoding of the file. Default is 'utf-8'. 36 37 Returns: 38 dict: The JSON data read from the file. 39 """ 40 with open(path_to_json, encoding=encoding) as f: 41 data = json.load(f) 42 return data
Function to read JSON data from a file.
Args: path_to_json (str): The path to the JSON file. encoding (str): The encoding of the file. Default is 'utf-8'.
Returns: dict: The JSON data read from the file.
def
write_to_json_file( dict: dict, file_path: str, encoding: str = 'utf-8', indent: int = 2) -> None:
45def write_to_json_file( 46 dict: dict, file_path: str, encoding: str = "utf-8", indent: int = 2 47) -> None: 48 """ 49 Function to write JSON data to a file. 50 51 Args: 52 dict (dict): The dictionary to be written to the file as JSON. 53 file_path (str): The path to the output JSON file. 54 encoding (str): The encoding of the file. Default is 'utf-8'. 55 indent (int): The number of spaces to use for indentation. Default is 2. 56 """ 57 json_object = json.dumps(dict, indent=indent) 58 59 with open(file_path, "w", encoding=encoding) as outfile: 60 outfile.write(json_object)
Function to write JSON data to a file.
Args: dict (dict): The dictionary to be written to the file as JSON. file_path (str): The path to the output JSON file. encoding (str): The encoding of the file. Default is 'utf-8'. indent (int): The number of spaces to use for indentation. Default is 2.