Skip to main content

Utility library for serializing/deserializing custom Python objects to/from JSON.

Project description

pyobjson - Python Object JSON Tool

Utility library for serializing/deserializing custom Python objects to/from JSON.

GitHub release (latest SemVer) GitHub tag (latest SemVer) GitHub Workflow Status (with event)

PyPI PyPI PyPI PyPI


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 a pyobjson-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 a pyobjson-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) and PythonObjectJson.load_from_json_file(json_file_path) methods to save/load your custom Python classes to JSON files.

MongoDB

PostgreSQL

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyobjson-4.2.1.tar.gz (3.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyobjson-4.2.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pyobjson-4.2.1.tar.gz.

File metadata

  • Download URL: pyobjson-4.2.1.tar.gz
  • Upload date:
  • Size: 3.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for pyobjson-4.2.1.tar.gz
Algorithm Hash digest
SHA256 9e5bfdafff9f3b0d531b8ebb796d4344d0c91dbfb733b12dea9d24d92d272ab7
MD5 0fe3219ffe70e4dbaee486cdc5373f8e
BLAKE2b-256 0dfb2c998bebac192d72f4ca991e74720dc572333a50c23aadd26eb09b969602

See more details on using hashes here.

File details

Details for the file pyobjson-4.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyobjson-4.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for pyobjson-4.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ed633118c2fc6359b5a9178421396a4da3f5ca79cca652ea27c12996e77d1da
MD5 9363d2da4b72178484096b3fe8cd71f9
BLAKE2b-256 de24d432ab8353770e2b112639abd55eec759381624ef29dd3e1b6fb32af8d02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page