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
 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,
46    file_path: str,
47    encoding: str = "utf-8",
48    indent: int = 2,
49    sort_keys: bool = False,
50) -> None:
51    """
52    Function to write JSON data to a file.
53
54    Args:
55    dict (dict): The dictionary to be written to the file as JSON.
56    file_path (str): The path to the output JSON file.
57    encoding (str): The encoding of the file. Default is 'utf-8'.
58    indent (int): The number of spaces to use for indentation. Default is 2.
59    sort_keys (bool): Whether to sort the keys in the output JSON. Default is False.
60    """
61    json_object = json.dumps(dict, indent=indent, sort_keys=sort_keys)
62
63    with open(file_path, "w", encoding=encoding) as outfile:
64        outfile.write(json_object)
65
66
67def sort_json_to_str(data: dict) -> str:
68    """Function to return a JSON string with sorted keys.
69
70    Args:
71        data (dict): The dictionary to be converted to a JSON string.
72    """
73    return json.dumps(data, sort_keys=True)
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, sort_keys: bool = False) -> None:
45def write_to_json_file(
46    dict: dict,
47    file_path: str,
48    encoding: str = "utf-8",
49    indent: int = 2,
50    sort_keys: bool = False,
51) -> None:
52    """
53    Function to write JSON data to a file.
54
55    Args:
56    dict (dict): The dictionary to be written to the file as JSON.
57    file_path (str): The path to the output JSON file.
58    encoding (str): The encoding of the file. Default is 'utf-8'.
59    indent (int): The number of spaces to use for indentation. Default is 2.
60    sort_keys (bool): Whether to sort the keys in the output JSON. Default is False.
61    """
62    json_object = json.dumps(dict, indent=indent, sort_keys=sort_keys)
63
64    with open(file_path, "w", encoding=encoding) as outfile:
65        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. sort_keys (bool): Whether to sort the keys in the output JSON. Default is False.

def sort_json_to_str(data: dict) -> str:
68def sort_json_to_str(data: dict) -> str:
69    """Function to return a JSON string with sorted keys.
70
71    Args:
72        data (dict): The dictionary to be converted to a JSON string.
73    """
74    return json.dumps(data, sort_keys=True)

Function to return a JSON string with sorted keys.

Args: data (dict): The dictionary to be converted to a JSON string.