Skip to content

Add support for serde-json arbitrary_precision #101

@jpmckinney

Description

@jpmckinney

When the arbitrary_precision feature is enabled, pythonize serializes all numbers like {'$serde_json::private::Number': '2'} instead of 2.

To reproduce

Cargo.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

[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

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

uv venv
uv pip install maturin
uv run maturin develop

Run

$ 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:

$ 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions