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.

Documentation

 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
 8
 9
10def _json_to_string(value_json):
11    return json.dumps(value_json, indent=0)
12
13
14def are_same(settings1_json_str, settings2_json_str):
15    """Function to compare two JSON objects, ignoring differences in whitespace."""
16
17    # parse and serialize to ignore any formatting differences:
18    def _parse_and_serialize(json_str):
19        value_json = json.loads(json_str)
20        return _json_to_string(value_json)
21
22    return _parse_and_serialize(settings1_json_str) == _parse_and_serialize(
23        settings2_json_str
24    )
25
26
27def read_from_json_file(path_to_json, encoding="utf-8"):
28    """
29    Function to read JSON data from a file.
30
31    Args:
32    path_to_json (str): The path to the JSON file.
33    encoding (str): The encoding of the file. Default is 'utf-8'.
34
35    Returns:
36    dict: The JSON data read from the file.
37    """
38    with open(path_to_json, encoding=encoding) as f:
39        data = json.load(f)
40        return data
41
42
43def write_to_json_file(dict, file_path, encoding="utf-8", indent=2):
44    """
45    Function to write JSON data to a file.
46
47    Args:
48    dict (dict): The dictionary to be written to the file as JSON.
49    file_path (str): The path to the output JSON file.
50    encoding (str): The encoding of the file. Default is 'utf-8'.
51    indent (int): The number of spaces to use for indentation. Default is 2.
52    """
53    json_object = json.dumps(dict, indent=indent)
54
55    with open(file_path, "w", encoding=encoding) as outfile:
56        outfile.write(json_object)
def are_same(settings1_json_str, settings2_json_str):
15def are_same(settings1_json_str, settings2_json_str):
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):
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    )

Function to compare two JSON objects, ignoring differences in whitespace.

def read_from_json_file(path_to_json, encoding='utf-8'):
28def read_from_json_file(path_to_json, encoding="utf-8"):
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

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, file_path, encoding='utf-8', indent=2):
44def write_to_json_file(dict, file_path, encoding="utf-8", indent=2):
45    """
46    Function to write JSON data to a file.
47
48    Args:
49    dict (dict): The dictionary to be written to the file as JSON.
50    file_path (str): The path to the output JSON file.
51    encoding (str): The encoding of the file. Default is 'utf-8'.
52    indent (int): The number of spaces to use for indentation. Default is 2.
53    """
54    json_object = json.dumps(dict, indent=indent)
55
56    with open(file_path, "w", encoding=encoding) as outfile:
57        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.