Stranger6667 / jsonschema

A high-performance JSON Schema validator for Rust
https://docs.rs/jsonschema
MIT License
526 stars 94 forks source link

Feature: Support decimal values #319

Open wolfgangwalther opened 2 years ago

wolfgangwalther commented 2 years ago

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 commented 2 years ago

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:

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.