JSON Encoder
Class to handle encoding and decoding of JSON data with special handling for datetime objects and enumerations.
decode_object(obj)
staticmethod
Convert the object back to its original form, including date and datetime objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
obj
|
The object (list, dict, etc.) to convert. |
required |
Returns:
Name | Type | Description |
---|---|---|
object |
obj
|
The original object. |
deserialise_from_json(json_string)
staticmethod
Deserialises the JSON string to an object, converting as needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_string
|
str
|
The JSON string to deserialise. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the data cannot be deserialized. |
Returns:
Name | Type | Description |
---|---|---|
return_data |
object
|
The deserialized object. |
read_from_file(file_path)
staticmethod
Reads the JSON data from a file and decodes it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
Path
|
The path to the JSON file. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the data cannot be read or decoded. |
Returns:
Name | Type | Description |
---|---|---|
dict |
object | None
|
The decoded JSON data or None if the file does not exist. |
ready_dict_for_json(data)
staticmethod
Prepares a dict or list for JSON serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
object
|
The data to prepare. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the data cannot be prepared. |
Returns:
Name | Type | Description |
---|---|---|
object |
object
|
A dict or list ready for export to JSON. |
save_to_file(data, file_path)
staticmethod
Saves the date to a JSON file, converting as needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
object
|
The data to prepare. |
required |
file_path
|
Path
|
The path to the JSON file to be created. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the data cannot be serialized. |
Returns:
Name | Type | Description |
---|---|---|
result |
bool
|
True if the pricing data was saved, False if not. |
serialise_to_json(data)
staticmethod
Serialises the data to a JSON string, converting as needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
object
|
The data to prepare. |
required |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the data cannot be serialized. |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The JSON string representation of the data. |