diff --git a/openjson/__init__.py b/openjson/__init__.py index cfb22d6..b29979a 100644 --- a/openjson/__init__.py +++ b/openjson/__init__.py @@ -1,9 +1,7 @@ # Dunder attributes -__version__ = "v0.0.0" # update setup.py +__version__ = "v0.1.0" # update setup.py __author__ = "Jordan Welsman" -from .encoding import * -from .loading import * -from .saving import * +from .api import * -__all__ = encoding.__all__, loading.__all__, saving.__all__ \ No newline at end of file +__all__ = api.__all__ \ No newline at end of file diff --git a/openjson/api.py b/openjson/api.py new file mode 100644 index 0000000..8e68960 --- /dev/null +++ b/openjson/api.py @@ -0,0 +1,14 @@ +from openjson.encoding import _encode + +__all__ = ['stringify'] + +def stringify(object: dict) -> str: + string: str = "{" + for index, item in enumerate(object): + if index != len(object) - 1: + string += f"{_encode(key=item, value=object[item])}, " + else: + string += f"{_encode(key=item, value=object[item])}" + + string += "}" + return string \ No newline at end of file diff --git a/openjson/encoding/__init__.py b/openjson/encoding/__init__.py index b680692..ec295fc 100644 --- a/openjson/encoding/__init__.py +++ b/openjson/encoding/__init__.py @@ -1 +1,5 @@ -__all__ = [] \ No newline at end of file +# Import submodule files so +# functions are usable at +# 'from openjson import _' level. +from .encode import * +__all__ = encode.__all__ \ No newline at end of file diff --git a/openjson/encoding/encode.py b/openjson/encoding/encode.py new file mode 100644 index 0000000..f9cd96b --- /dev/null +++ b/openjson/encoding/encode.py @@ -0,0 +1,69 @@ +# External class visibility +__all__ = ['_encode_object', '_encode_array', '_encode_number_int', '_encode_number_real', '_encode_string', '_encode_boolean', '_encode_null', '_encode'] + +def _encode_object(key: str, value: dict): + string = f"\"{key}\": " + "{" + for index, item in enumerate(value): + if index != len(value) - 1: + string += f"{_encode(item, value[item])}, " + else: + string += f"{_encode(item, value[item])}" + string += "}" + return string + + +def _encode_array(key: str, value: list): + string = f"\"{key}\": [" + for index, item in enumerate(value): + if index != len(value) - 1: + string += f"\"{item}\", " + else: + string += f"\"{item}\"" + string += "]" + return string + + +def _encode_number_int(key: str, value: int): + return f"\"{key}\": {value}" + + +def _encode_number_real(key: str, value: int, precision: float = None): + if precision is not None: + return f"\"{key}\": {round(value, precision)}" + else: + return f"\"{key}\": {value}" + + +def _encode_string(key: str, value: str): + return f"\"{key}\": \"{value}\"" + + +def _encode_boolean(key: str, value: bool): + if value is True: + return f"\"{key}\": true" + if value is False: + return f"\"{key}\": false" + + +def _encode_null(key: str): + return f"\"{key}\": null" + + +def _encode(key: str, value: object, int_precision: int = None) -> str: + match type(value).__name__: + case "dict": + return _encode_object(key=key, value=value) + case "list": + return _encode_array(key=key, value=value) + case "int": + return _encode_number_int(key=key, value=value) + case "float": + return _encode_number_real(key=key, value=value, precision=int_precision) + case "str": + return _encode_string(key=key, value=value) + case "bool": + return _encode_boolean(key=key, value=value) + case "NoneType": + return _encode_null(key=key) + case other: + raise NotImplementedError(f"Type \"{other}\" is not supported.") diff --git a/setup.py b/setup.py index 811d53a..f11ab41 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup # Arguments -version = "0.0.0" # update __init__.py +version = "0.1.0" # update __init__.py python_version = ">=3.10" # Long description from README.md