Add support for serde-json arbitrary_precision
When the `arbitrary_precision` feature is enabled, pythonize serializes all numbers like `{'$serde_json::private::Number': '2'}` instead of `2`.
## To reproduce
### Cargo.toml
```toml
[package]
name = "pythonize_bug"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.27", features = ["extension-module"] }
pythonize = "0.27"
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
```
### pyproject.toml
```toml
[build-system]
requires = ["maturin>=1.4,<2.0"]
build-backend = "maturin"
[project]
name = "pythonize_bug"
version = "0.1.0"
[tool.maturin]
module-name = "pythonize_bug"
```
### src/lib.rs
```rust
use pyo3::prelude::*;
use pythonize::{depythonize, pythonize};
use serde_json::Value;
#[pyfunction]
fn test_roundtrip<'py>(py: Python<'py>, obj: Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
let value: Value = depythonize(&obj)?;
Ok(pythonize(py, &value)?)
}
#[pymodule]
fn pythonize_bug(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(test_roundtrip, m)?)?;
Ok(())
}
```
### Install
```bash
uv venv
uv pip install maturin
uv run maturin develop
```
### Run
```shell-session
$ uv run python -c 'from pythonize_bug import test_roundtrip; print(test_roundtrip(18446744073709551615))'
{'$serde_json::private::Number': '18446744073709551615'}
```
If `, features = ["arbitrary_precision"]` is removed from Cargo.toml and `uv run maturin develop` is run:
```shell-session
$ uv run python -c 'from pythonize_bug import test_roundtrip; print(test_roundtrip(18446744073709551615))'
Traceback (most recent call last):
File "<string>", line 1, in <module>
Exception: JSON number out of range
```
关闭于 2026-02-18 1 条评论