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

json-transformer should coerce doubles with zero fractional part to longs #986

Open
irigarae opened this issue Jan 5, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@irigarae
Copy link

irigarae commented Jan 5, 2024

Per the json schema specification integers also accept floating point numbers with zero fractional part:

Numbers with a zero fractional part are considered integers:

1.0
;; compliant with schema { "type": "integer" }

This breaks a bit when decoding with a json-transformer.

(m/validate :int (m/decode :int 3.0 mt/json-transformer))
#_=> false

I think it would be a nice addition to the json-decoders:

;; just an example, not taking into account clj/cljs nor NaN, etc.
(defn -float->long [x]
  (if (and (float? x) (zero? (rem x 1)))
    (long x)
    x))
(m/validate :int (m/decode :int 3.0 (mt/transformer {:decoders {:int -float->long}})))
#_=> true
;; same example
(m/decode [:int {:decode/json -float->long}] 3.0 mt/json-transformer) #_=> 3
(m/decode [:int {:decode/json -float->long}] 3.2 mt/json-transformer) #_=> 3.2
@ikitommi
Copy link
Member

thanks for the info, this should be fixed.

@ikitommi ikitommi added the enhancement New feature or request label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants