repo: init

This commit is contained in:
tretrauit 2024-03-15 19:02:03 +07:00
commit 0a431f0bd8
10 changed files with 426 additions and 0 deletions

176
.gitignore vendored Normal file
View File

@ -0,0 +1,176 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
### Python Patch ###
# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
poetry.toml
# ruff
.ruff_cache/
# LSP config files
pyrightconfig.json
# End of https://www.toptal.com/developers/gitignore/api/python

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 tretrauit
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

20
README.md Normal file
View File

@ -0,0 +1,20 @@
# asjsonp
Another simple (and stupid) JSON parser.
## About
This implements `.json` as described in RFC 4627, including support for all standard features and some extended features:
+ Comment support
+ NaN, Infinity and -Infinity support (like the standard library)
## Usage
## Benchmark
This is about 10x slower than Python `json` module, so don't expect anything from this module.
## License
[MIT](./LICENSE)

4
asjsonp/__init__.py Executable file
View File

@ -0,0 +1,4 @@
from asjsonp.module import loads
__all__ = ["loads"]

4
asjsonp/__main__.py Executable file
View File

@ -0,0 +1,4 @@
from asjsonp.module import loads
print(loads(open("test.json", "r").read()))

125
asjsonp/module.py Executable file
View File

@ -0,0 +1,125 @@
__all__ = ["loads"]
def js2py(obj: str):
"""
Convert a JSON string to a Python object.
This follows the simple approach defined in https://docs.python.org/3/library/json.html#encoders-and-decoders
"""
if obj == "true":
return True
elif obj == "false":
return False
elif obj == "null":
return None
elif obj == "NaN":
return float("nan")
elif obj == "Infinity":
return float("inf")
elif obj == "-Infinity":
return float("-inf")
elif obj.isnumeric():
return int(obj)
elif obj.lower().replace(".", "").replace("e", "").replace("-", "").isnumeric():
return float(obj)
else:
raise ValueError(f"Invalid JSON value: {obj}")
def loads(s: str):
result: list | dict = []
first_open: bool = False
multiple_object_root: bool = False
parents: list = []
current: dict | list = result
dict_current_key: str = None
dict_reading_value: str = None
# Set the value so Pylance can be happy.
current_text: str = ""
is_reading_string: bool = False
# Ignore the rest of the string line if it's a comment
prev_char: str = ""
ignore_rest: bool = False
def handle_reading_value():
nonlocal dict_reading_value
nonlocal dict_current_key
if dict_reading_value is not None and dict_reading_value.strip():
value = js2py(dict_reading_value)
if dict_current_key:
current[dict_current_key] = value
else:
current.append(value)
dict_reading_value = None
dict_current_key = None
for char in s:
if ignore_rest:
if char == "\n":
ignore_rest = False
elif is_reading_string:
# Handle string closing quote
if char == '"':
is_reading_string = False
if isinstance(current, dict):
if not dict_current_key:
dict_current_key = current_text
else:
current[dict_current_key] = current_text
dict_current_key = None
dict_reading_value = None
else:
current.append(current_text)
dict_reading_value = None
current_text = ""
else:
current_text += char
# Handle object & array types
elif char in ["{", "["]:
obj: dict | list
match char:
case "{":
if not first_open:
first_open = True
obj = {}
case "[":
if not first_open:
first_open = True
multiple_object_root = True
obj = []
if isinstance(current, list):
current.append(obj)
elif isinstance(current, dict):
current[dict_current_key] = obj
parents.append(current)
# Reference to the current dict
if isinstance(current, list):
current = current[-1]
elif isinstance(current, dict):
current = current[dict_current_key]
dict_current_key = None
# Handle object and array closing bracket
elif char in ["}", "]"]:
# Switch reference back to the parent dict
handle_reading_value()
current = parents.pop()
# Handle string opening quote
elif char == '"':
is_reading_string = True
current_text = ""
elif char == ":":
dict_reading_value = ""
elif char == ",":
handle_reading_value()
elif char == "/":
if prev_char == "/":
ignore_rest = True
else:
if dict_reading_value is not None:
if char.strip():
dict_reading_value += char
prev_char = char
if not multiple_object_root:
result = result[0]
return result

33
benchmark/__main__.py Normal file
View File

@ -0,0 +1,33 @@
import asjsonp
import json
import time
import sys
def test_asjsonp(text: str, count: int):
start = time.perf_counter_ns()
for _ in range(count):
asjsonp.loads(text)
end = time.perf_counter_ns()
print(f"asjsonp.loads: {(end - start) / (1000 ** 2)}ms")
def test_json(text: str, count: int):
start = time.perf_counter_ns()
for _ in range(count):
json.loads(text)
end = time.perf_counter_ns()
print(f"json.loads: {(end - start) / (1000 ** 2)}ms")
input_file = "./benchmark/data/api.opensource.org.json"
count = 1000
for i, arg in enumerate(sys.argv[1:]):
if arg == "-i":
input_file = sys.argv[i + 2]
elif arg == "-c":
count = int(sys.argv[i + 2])
text = open(input_file, "r").read()
print(f"Benchmarking file {input_file} with {count} iterations")
test_asjsonp(text, count)
test_json(text, count)

File diff suppressed because one or more lines are too long

15
pyproject.toml Executable file
View File

@ -0,0 +1,15 @@
[tool.poetry]
name = "asjsonp"
version = "0.1.0"
description = "Another simple JSON parser"
authors = ["tretrauit <tretrauit@gmail.com>"]
license = "MIT"
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

27
test.json Executable file
View File

@ -0,0 +1,27 @@
{
"object": {
"ubuntu": 16.04,
"gcc": "10",
"metadata": null,
"another-object": {
"oriented": "programming"
},
"a": "a",
"array": [
"sparkle",
"hanabi",
{
"chipi": "chapa"
},
[[[[[[[[[["among us"]]]]]]]]]]
]
},
"cc": "gcc",
"glxgears": 123,
"chai": "bon",
"sparkle-sex": true,
"nan": NaN,
"-infinity": -Infinity,
"infinity": Infinity,
"e_number": 123e-123
}