Skip to content

Commit

Permalink
Merge pull request #55 from Ngoguey42/fix-nan-json-encoding
Browse files Browse the repository at this point in the history
Fix JSON encoder regarding NaNs
  • Loading branch information
craigfe authored Apr 27, 2021
2 parents 17a1c2a + 421d47a commit 0031d4a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

- Add a JSON object combinator: `Json.assoc` (#53, @Ngoguey42)

- Drop the payload of NaN floating point values during JSON encoding. `-nan`
strings are not emitted any more. (#55, @Ngoguey42)

### 0.2.1 (2021-01-18)

- Support Ppxlib versions >= 0.18.0. (#35, @CraigFe)
Expand Down
4 changes: 3 additions & 1 deletion src/repr/type_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ module Encode = struct

let float e f =
match classify_float f with
| FP_nan | FP_infinite -> lexeme e (`String (string_of_float f))
| FP_nan -> lexeme e (`String "nan")
| FP_infinite when Float.sign_bit f -> lexeme e (`String "-inf")
| FP_infinite -> lexeme e (`String "inf")
| _ -> lexeme e (`Float f)

let int e i = float e (float_of_int i)
Expand Down
2 changes: 2 additions & 0 deletions test/repr/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ let test_json_option () =
x

let test_json_float () =
let x = T.to_json_string T.float (-.Float.nan) in
Alcotest.(check string) "-NaN to JSON" "\"nan\"" x;
let x = T.to_json_string T.float Float.nan in
Alcotest.(check string) "NaN to JSON" "\"nan\"" x;
let x = T.to_json_string T.float Float.infinity in
Expand Down

0 comments on commit 0031d4a

Please sign in to comment.