cells-json 0.3.0__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.
Potentially problematic release.
This version of cells-json might be problematic. Click here for more details.
- cells_json-0.3.0/PKG-INFO +15 -0
- cells_json-0.3.0/README.md +188 -0
- cells_json-0.3.0/cells/json/__init__.py +44 -0
- cells_json-0.3.0/cells/json/adapter.py +520 -0
- cells_json-0.3.0/cells/json/exceptions.py +154 -0
- cells_json-0.3.0/cells/json/serializer.py +335 -0
- cells_json-0.3.0/cells/json/utils.py +168 -0
- cells_json-0.3.0/cells/json/version.py +1 -0
- cells_json-0.3.0/cells_json.egg-info/PKG-INFO +15 -0
- cells_json-0.3.0/cells_json.egg-info/SOURCES.txt +16 -0
- cells_json-0.3.0/cells_json.egg-info/dependency_links.txt +1 -0
- cells_json-0.3.0/cells_json.egg-info/not-zip-safe +1 -0
- cells_json-0.3.0/cells_json.egg-info/requires.txt +1 -0
- cells_json-0.3.0/cells_json.egg-info/top_level.txt +1 -0
- cells_json-0.3.0/setup.cfg +4 -0
- cells_json-0.3.0/setup.py +48 -0
- cells_json-0.3.0/tests/test_performance.py +130 -0
- cells_json-0.3.0/tests/test_serializer.py +549 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cells-json
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Orjson-first JSON serialization utilities for the cells namespace
|
|
5
|
+
Author: HarmonSir
|
|
6
|
+
Author-email: git@pylab.me
|
|
7
|
+
License: MPL-2.0
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Requires-Dist: orjson
|
|
10
|
+
Dynamic: author
|
|
11
|
+
Dynamic: author-email
|
|
12
|
+
Dynamic: license
|
|
13
|
+
Dynamic: requires-dist
|
|
14
|
+
Dynamic: requires-python
|
|
15
|
+
Dynamic: summary
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# cells-json
|
|
2
|
+
|
|
3
|
+
通用 JSON 序列化工具,提供 `orjson` 优先的高性能序列化能力,并补齐 Python 项目中常见的复杂类型处理需求。
|
|
4
|
+
|
|
5
|
+
## 🚀 核心特性
|
|
6
|
+
|
|
7
|
+
- **orjson 优先**:默认使用 `orjson` 作为主后端,优先追求吞吐量和低延迟。
|
|
8
|
+
- **极致性能**:通过适配器模式和类型查找缓存,性能较标准库提升 3x - 10x。
|
|
9
|
+
- **全量类型支持**:原生支持 `datetime`、`Decimal`、`UUID`、`Enum`、`Path`、`numpy`、`pandas`、`set`、`tuple` 等。
|
|
10
|
+
- **工业级异常体系**:提供统一的 `JSONEncodeError` 和 `JSONDecodeError`,兼容标准库并保留完整上下文。
|
|
11
|
+
- **多后端适配**:保留 `backend="json"` 显式回退能力,适合需要标准库参数语义的场景。
|
|
12
|
+
- **防御性设计**:内置循环引用检测(支持标记字符串或严格报错)和显式参数控制。
|
|
13
|
+
- **便捷工具**:提供装饰器、基类和辅助函数简化日常开发。
|
|
14
|
+
|
|
15
|
+
## 📦 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install cells-json
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 💡 快速开始
|
|
22
|
+
|
|
23
|
+
### 1. 核心 API 导入
|
|
24
|
+
|
|
25
|
+
你可以直接从根包导入最常用的功能:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from cells.json import (
|
|
29
|
+
safe_json_dumps, # 安全序列化(推荐)
|
|
30
|
+
dump, load, # 文件流接口
|
|
31
|
+
dumps, loads, # 高性能适配器
|
|
32
|
+
UniversalSerializer, # 核心转换器
|
|
33
|
+
JSONEncodeError, # 编码异常
|
|
34
|
+
JSONDecodeError # 解码异常
|
|
35
|
+
)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. 通用序列化 (推荐)
|
|
39
|
+
|
|
40
|
+
`safe_json_dumps` 会自动处理常见复杂类型,并采用递归扫描以支持循环引用标记。
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from datetime import datetime
|
|
44
|
+
from cells.json import safe_json_dumps
|
|
45
|
+
|
|
46
|
+
data = {
|
|
47
|
+
"time": datetime.now(),
|
|
48
|
+
"tags": {"python", "json"},
|
|
49
|
+
"meta": {"version": "1.0"}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# 支持 indent 等标准参数
|
|
53
|
+
json_str = safe_json_dumps(data, indent=2)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 3. 高性能模式
|
|
57
|
+
|
|
58
|
+
使用 `dumps` 和 `loads` 直接走 `orjson` 主路径。该接口默认统一返回 `str` 类型。
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from cells.json import dumps, loads
|
|
62
|
+
|
|
63
|
+
# 默认优先使用 orjson
|
|
64
|
+
json_str = dumps({"key": "value"}, ensure_ascii=False)
|
|
65
|
+
obj = loads(json_str)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. 标准库风格导入
|
|
69
|
+
|
|
70
|
+
如果你希望在工程里用 `cells` 统一替代标准库入口,可以直接这样写:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from cells import json
|
|
74
|
+
|
|
75
|
+
payload = json.dumps({"name": "alice"}, ensure_ascii=False)
|
|
76
|
+
data = json.loads(payload)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
也支持实例化适配器:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from cells.json import JSONAdapter
|
|
83
|
+
|
|
84
|
+
json = JSONAdapter()
|
|
85
|
+
payload = json.dumps({"name": "alice"}, ensure_ascii=False)
|
|
86
|
+
data = json.loads(payload)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## 后端策略
|
|
90
|
+
|
|
91
|
+
`cells.json` 的接口形状接近标准库 `json`,但行为策略以增强能力为准,不承诺与标准库逐项等价。
|
|
92
|
+
|
|
93
|
+
- 默认 `backend="auto"` 会优先使用 `orjson`。
|
|
94
|
+
- `orjson` 能适配的参数会尽量适配。
|
|
95
|
+
- 无法安全适配的标准库参数会直接报错,并提示显式指定 `backend="json"`。
|
|
96
|
+
|
|
97
|
+
例如:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from decimal import Decimal
|
|
101
|
+
from cells.json import dumps, loads
|
|
102
|
+
|
|
103
|
+
# orjson 主路径支持的参数
|
|
104
|
+
payload = dumps({"b": 1, "a": 2}, ensure_ascii=False, sort_keys=True, indent=2)
|
|
105
|
+
|
|
106
|
+
# 需要标准库解码钩子时,显式指定 json backend
|
|
107
|
+
data = loads("{\"value\": 1.25}", parse_float=Decimal, backend="json")
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 当前参数策略
|
|
111
|
+
|
|
112
|
+
- `sort_keys`:在 `orjson` 主路径下已适配。
|
|
113
|
+
- `indent=2`:在 `orjson` 主路径下已适配。
|
|
114
|
+
- `skipkeys=True`:在 `orjson` 主路径下会先做键过滤,再编码。
|
|
115
|
+
- `separators=(",", ":")`:在 `orjson` 主路径下已适配。
|
|
116
|
+
- `check_circular=False`:在 `orjson` 主路径下接受,但不保证行为与标准库一致。
|
|
117
|
+
- `ensure_ascii=True`、`object_hook`、`parse_float`、`parse_int`、`parse_constant`、`object_pairs_hook`、自定义 `cls`:
|
|
118
|
+
需要显式使用 `backend="json"`。
|
|
119
|
+
|
|
120
|
+
## 🔍 场景化指南 (QA)
|
|
121
|
+
|
|
122
|
+
| 场景 | 推荐方案 | 说明 |
|
|
123
|
+
|:---------------|:-------------------------------------------------------|:------------------------------|
|
|
124
|
+
| **标准库报不可序列化** | `safe_json_dumps(data)` | 自动转换 datetime/Decimal/UUID 等 |
|
|
125
|
+
| **遇到未知类型必须报错** | `UniversalSerializer(strict=True).dumps(data)` | 严格校验,防止脏数据入库 |
|
|
126
|
+
| **未知类型自动降级为空** | `UniversalSerializer(ignore_unknown=True).dumps(data)` | 无法处理的字段转为 `null` |
|
|
127
|
+
| **存在循环引用** | `safe_json_dumps(data, fail_on_circular=False)` | 默认模式,返回 `<CircularReference>` |
|
|
128
|
+
| **循环引用必须报错** | `safe_json_dumps(data, fail_on_circular=True)` | 抛出 `CircularReferenceError` |
|
|
129
|
+
| **追求极致吞吐量** | `from cells.json import dumps` | 默认优先走 orjson 主路径 |
|
|
130
|
+
| **美化输出/美化日志** | `from cells.json.utils import prettify_json` | 带缩进和排序的格式化 |
|
|
131
|
+
| **需要标准库解析钩子** | `loads(data, parse_float=Decimal, backend="json")` | 显式切换到标准库后端 |
|
|
132
|
+
|
|
133
|
+
## 📊 性能基准 (Performance)
|
|
134
|
+
|
|
135
|
+
在 Python 3.13 环境下,对比标准库 `json` 的吞吐量表现:
|
|
136
|
+
|
|
137
|
+
| 场景 | 标准 `json.dumps` | `cells-json` (Universal) | 提升倍数 |
|
|
138
|
+
|:-----------------------|:----------------|:-------------------------|:----------|
|
|
139
|
+
| **简单字典** (2k keys) | ~5,800 ops/sec | **~57,000 ops/sec** | **~10x** |
|
|
140
|
+
| **复杂对象** (500 entries) | ~630 ops/sec | **~2,100 ops/sec** | **~3.4x** |
|
|
141
|
+
|
|
142
|
+
> *注:测试包含类型缓存优化。复杂对象场景包含大量 DateTime、Decimal 和 UUID 的混合处理。*
|
|
143
|
+
|
|
144
|
+
## 🛠️ 支持的数据类型
|
|
145
|
+
|
|
146
|
+
- **日期时间**: `datetime`, `date`, `time` (ISO 格式), `timedelta` (总秒数)。
|
|
147
|
+
- **数值与标识**: `Decimal` (float), `UUID` (str), `Enum` (value)。
|
|
148
|
+
- **科学计算**: `numpy` (ndarray/generic), `pandas` (DataFrame/Series)。
|
|
149
|
+
- **容器**: `set`, `tuple` (转换为 list)。
|
|
150
|
+
- **自定义对象**:
|
|
151
|
+
- 优先尝试 `to_dict()` 方法。
|
|
152
|
+
- 其次尝试 `__dict__` 或 `__slots__` 序列化。
|
|
153
|
+
|
|
154
|
+
## ⚠️ 异常处理
|
|
155
|
+
|
|
156
|
+
本库提供了一套完整的、与底层后端解耦的异常体系:
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
from cells.json import JSONEncodeError, JSONDecodeError, CircularReferenceError
|
|
160
|
+
|
|
161
|
+
try:
|
|
162
|
+
payload = dumps(complex_obj)
|
|
163
|
+
except CircularReferenceError as e:
|
|
164
|
+
print(f"检测到循环引用: {e.path}")
|
|
165
|
+
except JSONEncodeError as e:
|
|
166
|
+
print(f"编码失败,对象类型: {e.obj_type}")
|
|
167
|
+
|
|
168
|
+
try:
|
|
169
|
+
data = loads(invalid_str)
|
|
170
|
+
except JSONDecodeError as e:
|
|
171
|
+
# 保留了标准库的错误位置信息
|
|
172
|
+
print(f"解析失败 at pos {e.pos}: {e.msg}")
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
当 `orjson` 主路径不支持某个标准库参数时,会直接抛出 `TypeError`,并提示使用 `backend="json"`。
|
|
176
|
+
|
|
177
|
+
## 📂 辅助工具 (Utils)
|
|
178
|
+
|
|
179
|
+
如需使用以下辅助功能,请从 `cells.json.utils` 导入:
|
|
180
|
+
|
|
181
|
+
- `save_json(data, path)` / `load_json(path)`:快捷文件读写。
|
|
182
|
+
- `prettify_json(data)`:快速生成美化的 JSON 预览。
|
|
183
|
+
- `JsonSerializable` 基类:继承后可使用 `obj.to_json()`。
|
|
184
|
+
- `@json_serializable` 装饰器:为类动态注入 JSON 序列化能力。
|
|
185
|
+
|
|
186
|
+
## 📜 许可证
|
|
187
|
+
|
|
188
|
+
MPL-2.0
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""JSON 序列化工具模块
|
|
2
|
+
|
|
3
|
+
提供通用的 JSON 序列化能力,处理 Python 中常见的序列化问题:
|
|
4
|
+
- datetime/date 对象序列化
|
|
5
|
+
- Decimal 对象序列化
|
|
6
|
+
- UUID 对象序列化
|
|
7
|
+
- numpy 数组序列化
|
|
8
|
+
- 自定义对象的序列化
|
|
9
|
+
- 复杂数据结构的序列化
|
|
10
|
+
- 多种 JSON 后端支持(json、orjson)
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .adapter import dump, dumps, JSONAdapter, load, loads
|
|
14
|
+
from .exceptions import (
|
|
15
|
+
CircularReferenceError,
|
|
16
|
+
JSONDecodeError,
|
|
17
|
+
JSONEncodeError,
|
|
18
|
+
JSONSerializationError,
|
|
19
|
+
UnsupportedTypeError,
|
|
20
|
+
)
|
|
21
|
+
from .serializer import safe_json_dumps, UniversalSerializer
|
|
22
|
+
from .utils import prettify_json
|
|
23
|
+
from .version import __VERSION__
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
# 核心序列化器
|
|
28
|
+
"UniversalSerializer",
|
|
29
|
+
"safe_json_dumps",
|
|
30
|
+
# 异常类
|
|
31
|
+
"JSONSerializationError",
|
|
32
|
+
"JSONEncodeError",
|
|
33
|
+
"JSONDecodeError",
|
|
34
|
+
"CircularReferenceError",
|
|
35
|
+
"UnsupportedTypeError",
|
|
36
|
+
# 适配器(支持多种 JSON 后端)
|
|
37
|
+
"JSONAdapter",
|
|
38
|
+
"dump",
|
|
39
|
+
"dumps",
|
|
40
|
+
"load",
|
|
41
|
+
"loads",
|
|
42
|
+
"prettify_json",
|
|
43
|
+
"__VERSION__",
|
|
44
|
+
]
|