Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support decimal values #319

Open
wolfgangwalther opened this issue Nov 23, 2021 · 1 comment
Open

Feature: Support decimal values #319

wolfgangwalther opened this issue Nov 23, 2021 · 1 comment

Comments

@wolfgangwalther
Copy link
Contributor

I'm using jsonschema-rs inside PostgreSQL via plpython3u. When I use PG's TRANSFORM FOR TYPE JSONB instead of python's json.loads(...) floats are converted to decimal.Decimal.

This results in:

ValueError: Unsupported type: 'decimal.Decimal'

The error is thrown here:
https://github.com/Stranger6667/jsonschema-rs/blob/0515c955a484d0a33308c2ed8c0057c7f6fb84a1/bindings/python/src/ser.rs#L223-L226

Is it possible to support Decimal, too?

Float is serialized like this:
https://github.com/Stranger6667/jsonschema-rs/blob/0515c955a484d0a33308c2ed8c0057c7f6fb84a1/bindings/python/src/ser.rs#L124-L126

Is this something that needs to be supported in pyo3 first?

@Stranger6667
Copy link
Owner

Hi!

There could be multiple ways to approach this issue. The simplest one would be to utilize the arbitrary_precision feature of serde-json - in this case, any number will be parsed as a string. Roughly:

  • Enable this feature for the underlying Rust crate
  • Convert strings to some decimal representation (e.g. rust_decimal::Decimal) instead of using as_<type> convertors
  • Adapt the validation logic to work with decimal representation. For rust_decimal::Decimal many things will work out of the box, as it implements comparison (though, it might require some adaptation)

And it will hit performance for the common case (unless behind a feature flag) because it will use String in many cases where smaller types would work. Though, it is kind of a thing that needs to be implemented anyway to support working with large numbers without precision loss on the Rust side.

Answering your question

Is this something that needs to be supported in pyo3 first?

There is no requirement for this to be implemented on the pyo3 side. Though, we'll need to convert values manually :)

It might be also good to take a look at this issue first. Implementing it will allow us to use a specialized implementation for Python objects and maybe avoid unnecessary string conversions & using that abovementioned feature flag at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants