Skip to main content

A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references.

Project description

pyobjtojson

A lightweight Python library that simplifies the process of serializing any Python object into a JSON-friendly structure without getting tripped up by circular references. With built-in support for dataclasses, Pydantic (v1 & v2), and standard Python collections, pyobjtojson helps you convert your objects into a cycle-free, JSON-ready format for logging, storage, or data transfer.

Features

  • Automatic Circular Reference Detection
    Detects and replaces cyclical structures with "<circular reference>" to prevent infinite loops.
  • Broad Compatibility
    Works seamlessly with dictionaries, lists, custom classes, dataclasses, and Pydantic models (including both model_dump() from v2 and dict() from v1).
  • Non-Intrusive Serialization
    No special inheritance or overrides needed. Uses reflection and standard Python methods (__dict__, asdict(), to_dict(), etc.) where available.
  • Easy to Integrate
    Just call obj_to_json() on your data structure—no additional configuration required.

Installation

pip install pyobjtojson

Quickstart

1. Basic Usage

from pyobjtojson import obj_to_json

# A simple dictionary with lists
data = {
    "key1": "value1",
    "key2": [1, 2, 3],
    "nested": {"inner_key": "inner_value"}
}

json_obj = obj_to_json(data)  # Using json.dumps kwargs

Output (example):

{
  "key1": "value1",
  "key2": [
    1,
    2,
    3
  ],
  "nested": {
    "inner_key": "inner_value"
  }
}

2. Handling Circular References

from pyobjtojson import obj_to_json

a = {"name": "A"}
b = {"circular": a}
a["b"] = b  # Creates a circular reference

obj_to_json(a)

Output:

{
  "name": "A",
  "b": {
    "circular": {
      "name": "A",
      "b": "<circular reference>"
    }
  }
}

3. Working with Dataclasses and Pydantic

from dataclasses import dataclass
from pydantic import BaseModel
from pyobjtojson import obj_to_json

@dataclass
class MyDataClass:
    title: str
    value: int

class MyModel(BaseModel):
    name: str
    age: int

dataclass_instance = MyDataClass(title="Test", value=123)
pydantic_instance = MyModel(name="Alice", age=30)

obj = {
    "dataclass": dataclass_instance,
    "pydantic": pydantic_instance
}

obj_to_json(obj)

Output:

{
  "dataclass": {
    "title": "Test",
    "value": 123
  },
  "pydantic": {
    "name": "Alice",
    "age": 30
  }
}

API Reference

  • obj_to_json(obj) -> dict | list | Any
    Returns a cycle-free structure (nested dictionaries/lists) that is JSON-serializable.

Contributing

Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.

License

MIT License

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

pyobjtojson-0.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

pyobjtojson-0.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file pyobjtojson-0.2.tar.gz.

File metadata

  • Download URL: pyobjtojson-0.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pyobjtojson-0.2.tar.gz
Algorithm Hash digest
SHA256 a7a025192d359ecbbc644446410405b572bfd664efa5b87d89869a2db91bf066
MD5 349151ccca714441d3b297982615e0d7
BLAKE2b-256 6378b9c047c3a09c864fd1f16ac6ecef72622f2b624e6fe205f32f520319f93e

See more details on using hashes here.

File details

Details for the file pyobjtojson-0.2-py3-none-any.whl.

File metadata

  • Download URL: pyobjtojson-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for pyobjtojson-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 21fd463d134dfaf41700fb5facb5c16c838dd18891ee5785e4d9afc5c893cb55
MD5 aa2892e20ab973c9fb1ca2366e9362fe
BLAKE2b-256 50d1f9f52d2f6c6ef709d23bec032bfd6697cc8b06691c1228cf7e3126b13b64

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