pyobjtojson 0.2__tar.gz → 0.3__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.
- {pyobjtojson-0.2 → pyobjtojson-0.3}/PKG-INFO +3 -3
- {pyobjtojson-0.2 → pyobjtojson-0.3}/README.md +2 -2
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyobjtojson/__init__.py +64 -19
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyobjtojson.egg-info/PKG-INFO +3 -3
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyproject.toml +1 -1
- {pyobjtojson-0.2 → pyobjtojson-0.3}/LICENSE +0 -0
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyobjtojson.egg-info/SOURCES.txt +0 -0
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyobjtojson.egg-info/dependency_links.txt +0 -0
- {pyobjtojson-0.2 → pyobjtojson-0.3}/pyobjtojson.egg-info/top_level.txt +0 -0
- {pyobjtojson-0.2 → pyobjtojson-0.3}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: pyobjtojson
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3
|
|
4
4
|
Summary: A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references.
|
|
5
5
|
Author-email: "Carlos A. Planchón" <carlosandresplanchonprestes@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -27,7 +27,7 @@ A lightweight Python library that simplifies the process of serializing **any**
|
|
|
27
27
|
No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
|
|
28
28
|
- **Easy to Integrate**
|
|
29
29
|
Just call `obj_to_json()` on your data structure—no additional configuration required.
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
## Installation
|
|
32
32
|
|
|
33
33
|
```bash
|
|
@@ -74,7 +74,7 @@ a = {"name": "A"}
|
|
|
74
74
|
b = {"circular": a}
|
|
75
75
|
a["b"] = b # Creates a circular reference
|
|
76
76
|
|
|
77
|
-
obj_to_json(a)
|
|
77
|
+
obj_to_json(a, check_circular=True) # check_circular is True by default.
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
**Output**:
|
|
@@ -12,7 +12,7 @@ A lightweight Python library that simplifies the process of serializing **any**
|
|
|
12
12
|
No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
|
|
13
13
|
- **Easy to Integrate**
|
|
14
14
|
Just call `obj_to_json()` on your data structure—no additional configuration required.
|
|
15
|
-
|
|
15
|
+
|
|
16
16
|
## Installation
|
|
17
17
|
|
|
18
18
|
```bash
|
|
@@ -59,7 +59,7 @@ a = {"name": "A"}
|
|
|
59
59
|
b = {"circular": a}
|
|
60
60
|
a["b"] = b # Creates a circular reference
|
|
61
61
|
|
|
62
|
-
obj_to_json(a)
|
|
62
|
+
obj_to_json(a, check_circular=True) # check_circular is True by default.
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
**Output**:
|
|
@@ -4,23 +4,31 @@ import dataclasses
|
|
|
4
4
|
from collections.abc import Mapping, Sequence
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
def _serialize_for_json(obj, visited):
|
|
7
|
+
def _serialize_for_json(obj, visited, check_circular=True):
|
|
8
8
|
"""
|
|
9
9
|
Internal recursion logic that can handle circular references
|
|
10
|
-
using `visited`.
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
using `visited`. This includes careful exception handling so
|
|
11
|
+
partial failures don't break the entire serialization.
|
|
12
|
+
|
|
13
|
+
:param obj: The object to serialize.
|
|
14
|
+
:param visited: A set used to track visited objects (for cycle detection).
|
|
15
|
+
:param check_circular: Whether to check for and mark circular references.
|
|
16
|
+
:return: A JSON-serializable structure, or a string if it cannot be
|
|
17
|
+
converted more structurally.
|
|
13
18
|
"""
|
|
14
19
|
|
|
15
20
|
# If it's None, bool, int, float, or str, it’s already JSON-serializable.
|
|
16
21
|
if obj is None or isinstance(obj, (bool, int, float, str)):
|
|
17
22
|
return obj
|
|
18
23
|
|
|
19
|
-
# If we've already visited this object, it's a circular reference
|
|
20
24
|
obj_id = id(obj)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
visited.
|
|
25
|
+
|
|
26
|
+
# If circular checking is enabled, see if we've already
|
|
27
|
+
# visited this object.
|
|
28
|
+
if check_circular is True:
|
|
29
|
+
if obj_id in visited:
|
|
30
|
+
return "<circular reference>"
|
|
31
|
+
visited.add(obj_id)
|
|
24
32
|
|
|
25
33
|
# Handle Mapping (like dict). Build a new dict item by item,
|
|
26
34
|
# catching errors.
|
|
@@ -28,7 +36,9 @@ def _serialize_for_json(obj, visited):
|
|
|
28
36
|
result = {}
|
|
29
37
|
for key, value in obj.items():
|
|
30
38
|
try:
|
|
31
|
-
result[key] = _serialize_for_json(
|
|
39
|
+
result[key] = _serialize_for_json(
|
|
40
|
+
value, visited, check_circular=check_circular
|
|
41
|
+
)
|
|
32
42
|
except Exception as exc:
|
|
33
43
|
result[key] = f"<serialization error: {exc}>"
|
|
34
44
|
return result
|
|
@@ -38,7 +48,13 @@ def _serialize_for_json(obj, visited):
|
|
|
38
48
|
result = []
|
|
39
49
|
for index, item in enumerate(obj):
|
|
40
50
|
try:
|
|
41
|
-
result.append(
|
|
51
|
+
result.append(
|
|
52
|
+
_serialize_for_json(
|
|
53
|
+
obj=item,
|
|
54
|
+
visited=visited,
|
|
55
|
+
check_circular=check_circular
|
|
56
|
+
)
|
|
57
|
+
)
|
|
42
58
|
except Exception as exc:
|
|
43
59
|
result.append(f"<serialization error at index {index}: {exc}>")
|
|
44
60
|
return result
|
|
@@ -47,7 +63,11 @@ def _serialize_for_json(obj, visited):
|
|
|
47
63
|
if hasattr(obj, "model_dump") and callable(obj.model_dump):
|
|
48
64
|
try:
|
|
49
65
|
model_data = obj.model_dump()
|
|
50
|
-
return _serialize_for_json(
|
|
66
|
+
return _serialize_for_json(
|
|
67
|
+
obj=model_data,
|
|
68
|
+
visited=visited,
|
|
69
|
+
check_circular=check_circular
|
|
70
|
+
)
|
|
51
71
|
except Exception:
|
|
52
72
|
# Fall through to next check if this fails
|
|
53
73
|
...
|
|
@@ -56,7 +76,11 @@ def _serialize_for_json(obj, visited):
|
|
|
56
76
|
if hasattr(obj, "dict") and callable(obj.dict):
|
|
57
77
|
try:
|
|
58
78
|
dict_data = obj.dict()
|
|
59
|
-
return _serialize_for_json(
|
|
79
|
+
return _serialize_for_json(
|
|
80
|
+
obj=dict_data,
|
|
81
|
+
visited=visited,
|
|
82
|
+
check_circular=check_circular
|
|
83
|
+
)
|
|
60
84
|
except Exception:
|
|
61
85
|
# Fall through to next check if this fails
|
|
62
86
|
...
|
|
@@ -65,7 +89,11 @@ def _serialize_for_json(obj, visited):
|
|
|
65
89
|
if dataclasses.is_dataclass(obj):
|
|
66
90
|
try:
|
|
67
91
|
dc_data = dataclasses.asdict(obj)
|
|
68
|
-
return _serialize_for_json(
|
|
92
|
+
return _serialize_for_json(
|
|
93
|
+
obj=dc_data,
|
|
94
|
+
visited=visited,
|
|
95
|
+
check_circular=check_circular
|
|
96
|
+
)
|
|
69
97
|
except Exception:
|
|
70
98
|
# Fall through to next check if this fails
|
|
71
99
|
...
|
|
@@ -74,7 +102,11 @@ def _serialize_for_json(obj, visited):
|
|
|
74
102
|
if hasattr(obj, "to_dict") and callable(obj.to_dict):
|
|
75
103
|
try:
|
|
76
104
|
custom_dict_data = obj.to_dict()
|
|
77
|
-
return _serialize_for_json(
|
|
105
|
+
return _serialize_for_json(
|
|
106
|
+
obj=custom_dict_data,
|
|
107
|
+
visited=visited,
|
|
108
|
+
check_circular=check_circular
|
|
109
|
+
)
|
|
78
110
|
except Exception:
|
|
79
111
|
# Fall through to next check if this fails
|
|
80
112
|
...
|
|
@@ -82,10 +114,14 @@ def _serialize_for_json(obj, visited):
|
|
|
82
114
|
# If the object has a __dict__, recurse into that
|
|
83
115
|
if hasattr(obj, "__dict__"):
|
|
84
116
|
try:
|
|
85
|
-
return _serialize_for_json(
|
|
117
|
+
return _serialize_for_json(
|
|
118
|
+
obj=obj.__dict__,
|
|
119
|
+
visited=visited,
|
|
120
|
+
check_circular=check_circular
|
|
121
|
+
)
|
|
86
122
|
except Exception:
|
|
87
123
|
# Fall through to next check if this fails
|
|
88
|
-
|
|
124
|
+
pass
|
|
89
125
|
|
|
90
126
|
# Last resort: convert to string, but even this can fail
|
|
91
127
|
# if __str__ is broken
|
|
@@ -96,9 +132,18 @@ def _serialize_for_json(obj, visited):
|
|
|
96
132
|
return f"<serialization error: {exc}>"
|
|
97
133
|
|
|
98
134
|
|
|
99
|
-
def obj_to_json(obj):
|
|
135
|
+
def obj_to_json(obj, check_circular=True):
|
|
100
136
|
"""
|
|
101
137
|
Public-facing function that starts with a fresh visited set
|
|
102
|
-
to handle cycles. Calls the internal
|
|
138
|
+
to handle cycles (if `check_circular=True`). Calls the internal
|
|
139
|
+
_serialize_for_json.
|
|
140
|
+
|
|
141
|
+
:param obj: The object to serialize to JSON-like structures.
|
|
142
|
+
:param check_circular: If True, detect and mark circular references.
|
|
103
143
|
"""
|
|
104
|
-
|
|
144
|
+
visited = set()
|
|
145
|
+
return _serialize_for_json(
|
|
146
|
+
obj=obj,
|
|
147
|
+
visited=visited,
|
|
148
|
+
check_circular=check_circular
|
|
149
|
+
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.2
|
|
2
2
|
Name: pyobjtojson
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3
|
|
4
4
|
Summary: A Python library that simplifies serializing any Python object to JSON-friendly structures, gracefully handling circular references.
|
|
5
5
|
Author-email: "Carlos A. Planchón" <carlosandresplanchonprestes@gmail.com>
|
|
6
6
|
License: MIT License
|
|
@@ -27,7 +27,7 @@ A lightweight Python library that simplifies the process of serializing **any**
|
|
|
27
27
|
No special inheritance or overrides needed. Uses reflection and standard Python methods (`__dict__`, `asdict()`, `to_dict()`, etc.) where available.
|
|
28
28
|
- **Easy to Integrate**
|
|
29
29
|
Just call `obj_to_json()` on your data structure—no additional configuration required.
|
|
30
|
-
|
|
30
|
+
|
|
31
31
|
## Installation
|
|
32
32
|
|
|
33
33
|
```bash
|
|
@@ -74,7 +74,7 @@ a = {"name": "A"}
|
|
|
74
74
|
b = {"circular": a}
|
|
75
75
|
a["b"] = b # Creates a circular reference
|
|
76
76
|
|
|
77
|
-
obj_to_json(a)
|
|
77
|
+
obj_to_json(a, check_circular=True) # check_circular is True by default.
|
|
78
78
|
```
|
|
79
79
|
|
|
80
80
|
**Output**:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|