pyobjtojson 0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Carlos A. Planchón
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.2
2
+ Name: pyobjtojson
3
+ Version: 0.1
4
+ Summary: A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references.
5
+ Author-email: "Carlos A. Planchón" <carlosandresplanchonprestes@gmail.com>
6
+ License: MIT License
7
+ Project-URL: repository, https://github.com/carlosplanchon/pyobjtojson.git
8
+ Keywords: json,serialization,circular references,pydantic,dataclasses
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Software Development :: Libraries
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+
16
+ # pyobjtojson
17
+
18
+ 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.
19
+
20
+ ## Features
21
+
22
+ - **Automatic Circular Reference Detection**
23
+ Detects and replaces cyclical structures with `"<circular reference>"` to prevent infinite loops.
24
+ - **Broad Compatibility**
25
+ Works seamlessly with dictionaries, lists, custom classes, dataclasses, and Pydantic models (including both `model_dump()` from v2 and `dict()` from v1).
26
+ - **Non-Intrusive Serialization**
27
+ No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
28
+ - **Easy to Integrate**
29
+ Just call `obj_to_json()` on your data structure—no additional configuration required.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install pyobjtojson
35
+ ```
36
+
37
+ ## Quickstart
38
+
39
+ ### 1. Basic Usage
40
+
41
+ ```python
42
+ from pyobjtojson import obj_to_json
43
+
44
+ # A simple dictionary with lists
45
+ data = {
46
+ "key1": "value1",
47
+ "key2": [1, 2, 3],
48
+ "nested": {"inner_key": "inner_value"}
49
+ }
50
+
51
+ json_obj = obj_to_json(data) # Using json.dumps kwargs
52
+ ```
53
+
54
+ **Output** (example):
55
+ ```json
56
+ {
57
+ "key1": "value1",
58
+ "key2": [
59
+ 1,
60
+ 2,
61
+ 3
62
+ ],
63
+ "nested": {
64
+ "inner_key": "inner_value"
65
+ }
66
+ }
67
+ ```
68
+
69
+ ### 2. Handling Circular References
70
+ ```python
71
+ from pyobjtojson import obj_to_json
72
+
73
+ a = {"name": "A"}
74
+ b = {"circular": a}
75
+ a["b"] = b # Creates a circular reference
76
+
77
+ obj_to_json(a)
78
+ ```
79
+
80
+ **Output**:
81
+ ```json
82
+ {
83
+ "name": "A",
84
+ "b": {
85
+ "circular": {
86
+ "name": "A",
87
+ "b": "<circular reference>"
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### 3. Working with Dataclasses and Pydantic
94
+
95
+ ```python
96
+ from dataclasses import dataclass
97
+ from pydantic import BaseModel
98
+ from pyobjtojson import obj_to_json
99
+
100
+ @dataclass
101
+ class MyDataClass:
102
+ title: str
103
+ value: int
104
+
105
+ class MyModel(BaseModel):
106
+ name: str
107
+ age: int
108
+
109
+ dataclass_instance = MyDataClass(title="Test", value=123)
110
+ pydantic_instance = MyModel(name="Alice", age=30)
111
+
112
+ obj = {
113
+ "dataclass": dataclass_instance,
114
+ "pydantic": pydantic_instance
115
+ }
116
+
117
+ obj_to_json(obj)
118
+ ```
119
+
120
+ **Output**:
121
+ ```json
122
+ {
123
+ "dataclass": {
124
+ "title": "Test",
125
+ "value": 123
126
+ },
127
+ "pydantic": {
128
+ "name": "Alice",
129
+ "age": 30
130
+ }
131
+ }
132
+ ```
133
+
134
+ ## API Reference
135
+
136
+ - **`obj_to_json(obj) -> dict | list | Any`**
137
+ Returns a cycle-free structure (nested dictionaries/lists) that is JSON-serializable.
138
+
139
+ ## Contributing
140
+ Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.
141
+
142
+ ## License
143
+ [MIT License](LICENSE)
@@ -0,0 +1,128 @@
1
+ # pyobjtojson
2
+
3
+ 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.
4
+
5
+ ## Features
6
+
7
+ - **Automatic Circular Reference Detection**
8
+ Detects and replaces cyclical structures with `"<circular reference>"` to prevent infinite loops.
9
+ - **Broad Compatibility**
10
+ Works seamlessly with dictionaries, lists, custom classes, dataclasses, and Pydantic models (including both `model_dump()` from v2 and `dict()` from v1).
11
+ - **Non-Intrusive Serialization**
12
+ No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
13
+ - **Easy to Integrate**
14
+ Just call `obj_to_json()` on your data structure—no additional configuration required.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ pip install pyobjtojson
20
+ ```
21
+
22
+ ## Quickstart
23
+
24
+ ### 1. Basic Usage
25
+
26
+ ```python
27
+ from pyobjtojson import obj_to_json
28
+
29
+ # A simple dictionary with lists
30
+ data = {
31
+ "key1": "value1",
32
+ "key2": [1, 2, 3],
33
+ "nested": {"inner_key": "inner_value"}
34
+ }
35
+
36
+ json_obj = obj_to_json(data) # Using json.dumps kwargs
37
+ ```
38
+
39
+ **Output** (example):
40
+ ```json
41
+ {
42
+ "key1": "value1",
43
+ "key2": [
44
+ 1,
45
+ 2,
46
+ 3
47
+ ],
48
+ "nested": {
49
+ "inner_key": "inner_value"
50
+ }
51
+ }
52
+ ```
53
+
54
+ ### 2. Handling Circular References
55
+ ```python
56
+ from pyobjtojson import obj_to_json
57
+
58
+ a = {"name": "A"}
59
+ b = {"circular": a}
60
+ a["b"] = b # Creates a circular reference
61
+
62
+ obj_to_json(a)
63
+ ```
64
+
65
+ **Output**:
66
+ ```json
67
+ {
68
+ "name": "A",
69
+ "b": {
70
+ "circular": {
71
+ "name": "A",
72
+ "b": "<circular reference>"
73
+ }
74
+ }
75
+ }
76
+ ```
77
+
78
+ ### 3. Working with Dataclasses and Pydantic
79
+
80
+ ```python
81
+ from dataclasses import dataclass
82
+ from pydantic import BaseModel
83
+ from pyobjtojson import obj_to_json
84
+
85
+ @dataclass
86
+ class MyDataClass:
87
+ title: str
88
+ value: int
89
+
90
+ class MyModel(BaseModel):
91
+ name: str
92
+ age: int
93
+
94
+ dataclass_instance = MyDataClass(title="Test", value=123)
95
+ pydantic_instance = MyModel(name="Alice", age=30)
96
+
97
+ obj = {
98
+ "dataclass": dataclass_instance,
99
+ "pydantic": pydantic_instance
100
+ }
101
+
102
+ obj_to_json(obj)
103
+ ```
104
+
105
+ **Output**:
106
+ ```json
107
+ {
108
+ "dataclass": {
109
+ "title": "Test",
110
+ "value": 123
111
+ },
112
+ "pydantic": {
113
+ "name": "Alice",
114
+ "age": 30
115
+ }
116
+ }
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ - **`obj_to_json(obj) -> dict | list | Any`**
122
+ Returns a cycle-free structure (nested dictionaries/lists) that is JSON-serializable.
123
+
124
+ ## Contributing
125
+ Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.
126
+
127
+ ## License
128
+ [MIT License](LICENSE)
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env python3
2
+
3
+ import dataclasses
4
+ from collections.abc import Mapping, Sequence
5
+
6
+
7
+ def _serialize_for_json(obj, visited):
8
+ """
9
+ Internal recursion logic that can
10
+ handle circular references using `visited`.
11
+ """
12
+ # If it's None, bool, int, float, or str, it’s already serializable
13
+ if obj is None or isinstance(obj, (bool, int, float, str)):
14
+ return obj
15
+
16
+ # If we've seen this object already, it's a circular reference
17
+ obj_id = id(obj)
18
+ if obj_id in visited:
19
+ return "<circular reference>"
20
+ visited.add(obj_id)
21
+
22
+ # If it's a Mapping (like dict), recursively process
23
+ if isinstance(obj, Mapping):
24
+ return {
25
+ key: _serialize_for_json(value, visited)
26
+ for key, value in obj.items()
27
+ }
28
+
29
+ # If it's a Sequence (like list or tuple) but not a string
30
+ if isinstance(obj, Sequence) and not isinstance(obj, str):
31
+ return [_serialize_for_json(item, visited) for item in obj]
32
+
33
+ # Check for Pydantic v2's model_dump()
34
+ if hasattr(obj, "model_dump") and callable(obj.model_dump):
35
+ return _serialize_for_json(obj.model_dump(), visited)
36
+
37
+ # Check for Pydantic v1's dict()
38
+ if hasattr(obj, "dict") and callable(obj.dict):
39
+ return _serialize_for_json(obj.dict(), visited)
40
+
41
+ # If it's a dataclass, convert it using dataclasses.asdict()
42
+ if dataclasses.is_dataclass(obj):
43
+ return _serialize_for_json(dataclasses.asdict(obj), visited)
44
+
45
+ # If there's a custom .to_dict() method
46
+ if hasattr(obj, "to_dict") and callable(obj.to_dict):
47
+ return _serialize_for_json(obj.to_dict(), visited)
48
+
49
+ # If the object has a __dict__, serialize that
50
+ if hasattr(obj, "__dict__"):
51
+ return _serialize_for_json(obj.__dict__, visited)
52
+
53
+ # If we get here, just convert to string (as a last resort)
54
+ return str(obj)
55
+
56
+
57
+ def obj_to_json(obj):
58
+ """
59
+ Public-facing function that starts with a fresh visited set
60
+ to handle cycles. This calls the internal _serialize_for_json.
61
+ """
62
+ return _serialize_for_json(obj, visited=set())
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.2
2
+ Name: pyobjtojson
3
+ Version: 0.1
4
+ Summary: A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references.
5
+ Author-email: "Carlos A. Planchón" <carlosandresplanchonprestes@gmail.com>
6
+ License: MIT License
7
+ Project-URL: repository, https://github.com/carlosplanchon/pyobjtojson.git
8
+ Keywords: json,serialization,circular references,pydantic,dataclasses
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Software Development :: Libraries
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+
16
+ # pyobjtojson
17
+
18
+ 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.
19
+
20
+ ## Features
21
+
22
+ - **Automatic Circular Reference Detection**
23
+ Detects and replaces cyclical structures with `"<circular reference>"` to prevent infinite loops.
24
+ - **Broad Compatibility**
25
+ Works seamlessly with dictionaries, lists, custom classes, dataclasses, and Pydantic models (including both `model_dump()` from v2 and `dict()` from v1).
26
+ - **Non-Intrusive Serialization**
27
+ No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
28
+ - **Easy to Integrate**
29
+ Just call `obj_to_json()` on your data structure—no additional configuration required.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install pyobjtojson
35
+ ```
36
+
37
+ ## Quickstart
38
+
39
+ ### 1. Basic Usage
40
+
41
+ ```python
42
+ from pyobjtojson import obj_to_json
43
+
44
+ # A simple dictionary with lists
45
+ data = {
46
+ "key1": "value1",
47
+ "key2": [1, 2, 3],
48
+ "nested": {"inner_key": "inner_value"}
49
+ }
50
+
51
+ json_obj = obj_to_json(data) # Using json.dumps kwargs
52
+ ```
53
+
54
+ **Output** (example):
55
+ ```json
56
+ {
57
+ "key1": "value1",
58
+ "key2": [
59
+ 1,
60
+ 2,
61
+ 3
62
+ ],
63
+ "nested": {
64
+ "inner_key": "inner_value"
65
+ }
66
+ }
67
+ ```
68
+
69
+ ### 2. Handling Circular References
70
+ ```python
71
+ from pyobjtojson import obj_to_json
72
+
73
+ a = {"name": "A"}
74
+ b = {"circular": a}
75
+ a["b"] = b # Creates a circular reference
76
+
77
+ obj_to_json(a)
78
+ ```
79
+
80
+ **Output**:
81
+ ```json
82
+ {
83
+ "name": "A",
84
+ "b": {
85
+ "circular": {
86
+ "name": "A",
87
+ "b": "<circular reference>"
88
+ }
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### 3. Working with Dataclasses and Pydantic
94
+
95
+ ```python
96
+ from dataclasses import dataclass
97
+ from pydantic import BaseModel
98
+ from pyobjtojson import obj_to_json
99
+
100
+ @dataclass
101
+ class MyDataClass:
102
+ title: str
103
+ value: int
104
+
105
+ class MyModel(BaseModel):
106
+ name: str
107
+ age: int
108
+
109
+ dataclass_instance = MyDataClass(title="Test", value=123)
110
+ pydantic_instance = MyModel(name="Alice", age=30)
111
+
112
+ obj = {
113
+ "dataclass": dataclass_instance,
114
+ "pydantic": pydantic_instance
115
+ }
116
+
117
+ obj_to_json(obj)
118
+ ```
119
+
120
+ **Output**:
121
+ ```json
122
+ {
123
+ "dataclass": {
124
+ "title": "Test",
125
+ "value": 123
126
+ },
127
+ "pydantic": {
128
+ "name": "Alice",
129
+ "age": 30
130
+ }
131
+ }
132
+ ```
133
+
134
+ ## API Reference
135
+
136
+ - **`obj_to_json(obj) -> dict | list | Any`**
137
+ Returns a cycle-free structure (nested dictionaries/lists) that is JSON-serializable.
138
+
139
+ ## Contributing
140
+ Contributions, bug reports, and feature requests are welcome! Feel free to open an issue or submit a pull request.
141
+
142
+ ## License
143
+ [MIT License](LICENSE)
@@ -0,0 +1,8 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ pyobjtojson/__init__.py
5
+ pyobjtojson.egg-info/PKG-INFO
6
+ pyobjtojson.egg-info/SOURCES.txt
7
+ pyobjtojson.egg-info/dependency_links.txt
8
+ pyobjtojson.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ pyobjtojson
@@ -0,0 +1,27 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [options]
6
+ package_dir = "pyobjtojson"
7
+ packages = ["pyobjtojson"]
8
+
9
+ [project]
10
+ name = "pyobjtojson"
11
+ version = "0.1"
12
+ authors = [
13
+ {name = "Carlos A. Planchón", email = "carlosandresplanchonprestes@gmail.com"},
14
+ ]
15
+ description = "A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references."
16
+ classifiers = [
17
+ "Intended Audience :: Developers",
18
+ "Topic :: Software Development :: Libraries",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Programming Language :: Python :: 3.13",
21
+ ]
22
+ license = {text = "MIT License"}
23
+ keywords = ["json", "serialization", "circular references", "pydantic", "dataclasses"]
24
+ readme = "README.md"
25
+
26
+ [project.urls]
27
+ repository = "https://github.com/carlosplanchon/pyobjtojson.git"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+