-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from JordanWelsman/develop
To-do list complete. Merging now.
- Loading branch information
Showing
5 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters