cells-json 0.3.1__tar.gz → 0.3.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cells-json
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Orjson-first JSON serialization utilities for the cells namespace
5
5
  Author: HarmonSir
6
6
  Author-email: git@pylab.me
@@ -31,6 +31,36 @@ _ORJSON_SAFE_LOADS_KWARGS = set()
31
31
  _JSON_BASIC_KEY_TYPES = (str, int, float, bool, type(None))
32
32
 
33
33
 
34
+ def _escape_codepoint_for_json_ascii(character: str) -> str:
35
+ codepoint = ord(character)
36
+ if codepoint <= 0xFFFF:
37
+ return f"\\u{codepoint:04x}"
38
+
39
+ codepoint -= 0x10000
40
+ high_surrogate = 0xD800 + (codepoint >> 10)
41
+ low_surrogate = 0xDC00 + (codepoint & 0x3FF)
42
+ return f"\\u{high_surrogate:04x}\\u{low_surrogate:04x}"
43
+
44
+
45
+ def _ensure_ascii_json_text(json_text: str) -> str:
46
+ if json_text.isascii():
47
+ return json_text
48
+
49
+ ascii_chunks: list[str] = []
50
+ for character in json_text:
51
+ if character.isascii():
52
+ ascii_chunks.append(character)
53
+ continue
54
+ ascii_chunks.append(_escape_codepoint_for_json_ascii(character))
55
+ return "".join(ascii_chunks)
56
+
57
+
58
+ def _ensure_ascii_json_bytes(json_bytes: bytes) -> bytes:
59
+ if json_bytes.isascii():
60
+ return json_bytes
61
+ return _ensure_ascii_json_text(json_bytes.decode("utf-8")).encode("ascii")
62
+
63
+
34
64
  def _raise_orjson_parameter_error(function_name: str, parameter_name: str) -> None:
35
65
  raise TypeError(
36
66
  f"{function_name}() does not support parameter '{parameter_name}' with the active orjson backend. "
@@ -70,7 +100,6 @@ def _is_default_json_decoder(cls: Any) -> bool:
70
100
  def _validate_orjson_dumps_parameters(
71
101
  *,
72
102
  skipkeys: bool,
73
- ensure_ascii: bool,
74
103
  check_circular: bool,
75
104
  allow_nan: bool,
76
105
  cls: Any,
@@ -80,8 +109,6 @@ def _validate_orjson_dumps_parameters(
80
109
  extra_kwargs: dict[str, Any],
81
110
  ) -> int:
82
111
  option = 0
83
- if ensure_ascii:
84
- _raise_orjson_parameter_error("dumps", "ensure_ascii")
85
112
  if not allow_nan:
86
113
  _raise_orjson_parameter_error("dumps", "allow_nan")
87
114
  if not _is_default_json_encoder(cls):
@@ -179,7 +206,6 @@ class JSONAdapter:
179
206
  if HAS_ORJSON and self._backend != "json":
180
207
  validated_option = _validate_orjson_dumps_parameters(
181
208
  skipkeys=skipkeys,
182
- ensure_ascii=ensure_ascii,
183
209
  check_circular=check_circular,
184
210
  allow_nan=allow_nan,
185
211
  cls=cls,
@@ -195,6 +221,8 @@ class JSONAdapter:
195
221
  option=validated_option if option is None else (option | validated_option),
196
222
  **kwargs,
197
223
  )
224
+ if ensure_ascii:
225
+ result = _ensure_ascii_json_bytes(result)
198
226
  else:
199
227
  result = self._json_dumps(
200
228
  obj,
@@ -0,0 +1 @@
1
+ __VERSION__ = "0.3.2"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cells-json
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Orjson-first JSON serialization utilities for the cells namespace
5
5
  Author: HarmonSir
6
6
  Author-email: git@pylab.me
@@ -444,21 +444,19 @@ def test_orjson_indent_and_sort_keys():
444
444
  print("✓ orjson 参数适配测试通过")
445
445
 
446
446
 
447
- def test_orjson_unsupported_dump_parameter_requires_json_backend():
448
- """测试不支持的编码参数会要求显式指定 json backend"""
449
- print("测试 dumps 不支持参数报错...")
447
+ def test_orjson_ensure_ascii_is_adapted():
448
+ """测试 ensure_ascii=True 在 orjson 主路径下会做兼容转换"""
449
+ print("测试 dumps ensure_ascii 适配...")
450
+ result = dumps({"name": "中文", "emoji": "😀"}, ensure_ascii=True)
451
+ assert result.isascii()
452
+ assert "\\u4e2d\\u6587" in result
453
+ assert "\\ud83d\\ude00" in result
454
+ parsed = loads(result)
455
+ assert parsed == {"name": "中文", "emoji": "😀"}
450
456
  if not HAS_ORJSON:
451
- result = dumps({"name": "中文"}, ensure_ascii=True)
452
- assert "\\u4e2d\\u6587" in result
453
- print("⊘ orjson 未安装,auto 已退回 json backend")
457
+ print("⊘ orjson 未安装,当前由 json backend 原生处理")
454
458
  return
455
- try:
456
- dumps({"name": "中文"}, ensure_ascii=True)
457
- assert False, "应该提示显式指定 json backend"
458
- except TypeError as exc:
459
- assert "backend=\"json\"" in str(exc)
460
- assert "ensure_ascii" in str(exc)
461
- print("✓ dumps 不支持参数报错测试通过")
459
+ print("✓ dumps ensure_ascii 适配测试通过")
462
460
 
463
461
 
464
462
  def test_orjson_skipkeys_is_adapted():
@@ -533,7 +531,7 @@ def run_all_tests():
533
531
  test_parse_float_requires_json_backend()
534
532
  test_adapter_instance_stdlib_signature()
535
533
  test_orjson_indent_and_sort_keys()
536
- test_orjson_unsupported_dump_parameter_requires_json_backend()
534
+ test_orjson_ensure_ascii_is_adapted()
537
535
  test_orjson_skipkeys_is_adapted()
538
536
  test_orjson_compact_separators_is_adapted()
539
537
  test_orjson_check_circular_false_is_accepted()
@@ -1 +0,0 @@
1
- __VERSION__ = "0.3.1"
File without changes
File without changes
File without changes