Skip to content

Commit

Permalink
Merge pull request #2 from JordanWelsman/develop
Browse files Browse the repository at this point in the history
To-do list complete. Merging now.
  • Loading branch information
JordanWelsman authored Mar 2, 2023
2 parents d64df74 + 17664b0 commit f7ff77d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
8 changes: 3 additions & 5 deletions openjson/__init__.py
Original file line number Diff line number Diff line change
@@ -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__
__all__ = api.__all__
14 changes: 14 additions & 0 deletions openjson/api.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion openjson/encoding/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
__all__ = []
# Import submodule files so
# functions are usable at
# 'from openjson import _' level.
from .encode import *
__all__ = encode.__all__
69 changes: 69 additions & 0 deletions openjson/encoding/encode.py
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f7ff77d

Please sign in to comment.