Files
api_utils_converse_v2/utils_v2/serialization/json_serializer.py
T

130 lines
5.2 KiB
Python

"""
AUTHOR:
Khushal P Soonderji
DATE:
Sunday 1st Sept. 2024.
OBJECTIVE:
To provide a way to convert any input data to serialized bytes, and back.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# To work with files:
from utils_v2.string import json
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** CLASSES ***
# ***** ****
# *****************************************************************************************************************
class JSONSerializer:
def __init__(self):
"""
Use this serializer when dealing with JSON-compatible data like direct JSON-strings, python dicts, and
python-lists. Beware that non-compatible data will cause either direct exceptions or unexpected behaviour.
"""
pass
@staticmethod
def serialize(data, encoding = "utf-8"):
"""
Serializes the data that is given to it.
The input has to be JSON-compatible.
:param data: The data to serialize.
:param encoding: The encoding to use.
:return: The bytes representing the data.
"""
# If the data is not already a JSON string, parse it. Then return it as bytes:
data = data if isinstance(data, str) else json.to_string(data, no_space = True)
return data.encode(encoding)
@staticmethod
def deserialize(data, encoding = "utf-8"):
"""
Deserializes the bytes that are given to it.
The input has to be JSON-compatible.
:param data: The bytes to deserialize.
:param encoding: The encoding to use.
:return: The data from the bytes that described it.
"""
data = data.decode(encoding)
return json.from_string(data)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass