Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
fix: encode float::Inf as null in json
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchneider committed Mar 7, 2023
1 parent fa2c51c commit c0b3b86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/io/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
array.iter(),
|x, buf| {
if let Some(x) = x {
if T::is_nan(*x) {
if T::is_nan(*x) || T::is_infinite(*x) {
buf.extend(b"null")
} else {
lexical_to_bytes_mut(*x, buf)
Expand Down
24 changes: 20 additions & 4 deletions tests/it/io/json/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@ fn int32() -> Result<()> {

#[test]
fn f32() -> Result<()> {
let array = Float32Array::from([Some(1.5), Some(2.5), Some(f32::NAN), None, Some(5.5)]);
let array = Float32Array::from([
Some(1.5),
Some(2.5),
Some(f32::NAN),
Some(f32::INFINITY),
Some(f32::NEG_INFINITY),
None,
Some(5.5),
]);

let expected = r#"[1.5,2.5,null,null,5.5]"#;
let expected = r#"[1.5,2.5,null,null,null,null,5.5]"#;

test!(array, expected)
}

#[test]
fn f64() -> Result<()> {
let array = Float64Array::from([Some(1.5), Some(2.5), Some(f64::NAN), None, Some(5.5)]);
let array = Float64Array::from([
Some(1.5),
Some(2.5),
Some(f64::NAN),
Some(f64::INFINITY),
Some(f64::NEG_INFINITY),
None,
Some(5.5),
]);

let expected = r#"[1.5,2.5,null,null,5.5]"#;
let expected = r#"[1.5,2.5,null,null,null,null,5.5]"#;

test!(array, expected)
}
Expand Down

0 comments on commit c0b3b86

Please sign in to comment.