pyobjson - Python Object JSON Tool¶
Utility library for serializing/deserializing custom Python objects to/from JSON.
Do you like the Python Object JSON Tool? Star the repository on GitHub and please consider helping support its ongoing development:
READ THE DOCS HERE!
Detailed documentation can be found at https://pyobjson.wrencode.dev.
Table of Contents¶
About¶
The Python Object JSON Tool is a utility library for serializing/deserializing custom Python objects to/from JSON.
Dependencies¶
The Python Object JSON Tool does not have any third-party dependencies to run the code. It has several development dependencies, which can be seen in the package pyproject.toml
.
Toolchain¶
The below tools and resources are used as part of pyobjson:
- uv - package management
- ruff - code linting
- bandit - code security
- make - Makefile build automation
- MkDocs - package documentation
- python-dotenv - programmatic access to environment variables defined in a
.env
file - pytest - code testing framework
- GitHub Actions - CI/CD
- act - GitHub Actions testing
Usage¶
The pyobjson
package is designed to be used as a base class/parent class/superclass to your own custom Python classes in order to provide built-in, convenient serialization/deserialization functionality. Child classes/subclasses of pyobjson.base.PythonObjectJson
will automatically have the following methods:
pyobjson.base.PythonObjectJson.serialize()
: Create a serializable dictionary from the class instance.pyobjson.base.PythonObjectJson.to_json_str()
: Serialize the class instance to a JSON string.pyobjson.base.PythonObjectJson.from_json_str(json_str)
: Load the class instance from apyobjson
-formatted JSON string.pyobjson.base.PythonObjectJson.save_to_json_file(json_file_path)
: Save the class instance to a JSON file.pyobjson.base.PythonObjectJson.load_from_json_file(json_file_path)
: Load the class instance from apyobjson
-formatted JSON file.
Please reference the documentation at https://pyobjson.wrencode.dev for more detailed usage.
Example¶
from pyobjson.base import PythonObjectJson
class ChildClass(PythonObjectJson):
def __init__(self):
super().__init__()
self.message = "Hello, World!"
class ParentClass(PythonObjectJson):
def __init__(self):
super().__init__()
self.child_classes = [ChildClass()]
parent_class = ParentClass()
print(parent_class.to_json_str())
Output¶
{
"__main__.parentclass": {
"collection:list.child_classes": [
{
"__main__.childclass": {
"message": "Hello, World!"
}
}
]
}
}
The above example shows how pyobjson
can be used to serialize arbitrary custom Python classes into JSON. Additionally, the above output JSON can be used to recreate an equivalent class instance by loading the JSON into a custom Python class instance.
Saving and Loading¶
The pyobjson.base.PythonObjectJson
parent class also provides built-in methods to save/load arbitrary custom Python classes to/from JSON in several ways.
JSON Files¶
- JSON files (using only Python built-in libraries): Use the
PythonObjectJson.save_to_json_file(json_file_path)
andPythonObjectJson.load_from_json_file(json_file_path)
methods to save/load your custom Python classes to JSON files.
MongoDB¶
PostgreSQL¶
- PostgreSQL (using
psycopg
): COMING SOON!