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

Fixed encoding of NaN to json #990

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/io/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ fn primitive_serializer<'a, T: NativeType + ToLexical>(
))
}

fn f64_serializer<'a>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use generics to have a single implementation;

fn float_serializer<'a, T>(
    array: &'a PrimitiveArray<T>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync>
where T: num_traits::Float + NativeType + lexical_core::ToLexical
{
    Box::new(BufStreamingIterator::new(
        array.iter(),
        |x, buf| {
            if let Some(x) = x {
                if T::is_nan(*x) {
                    buf.extend(b"null")
                } else {
                    lexical_to_bytes_mut(*x, buf)
                }
            } else {
                buf.extend(b"null")
            }
        },
        vec![],
    ))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, didn't know that was possible 🎉

array: &'a PrimitiveArray<f64>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Box::new(BufStreamingIterator::new(
array.iter(),
|x, buf| {
if let Some(x) = x {
if f64::is_nan(*x) {
buf.extend(b"null")
} else {
lexical_to_bytes_mut(*x, buf)
}
} else {
buf.extend(b"null")
}
},
vec![],
))
}

fn f32_serializer<'a>(
array: &'a PrimitiveArray<f32>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Box::new(BufStreamingIterator::new(
array.iter(),
|x, buf| {
if let Some(x) = x {
if f32::is_nan(*x) {
buf.extend(b"null")
} else {
lexical_to_bytes_mut(*x, buf)
}
} else {
buf.extend(b"null")
}
},
vec![],
))
}

fn utf8_serializer<'a, O: Offset>(
array: &'a Utf8Array<O>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Expand Down Expand Up @@ -196,8 +236,8 @@ pub(crate) fn new_serializer<'a>(
DataType::UInt16 => primitive_serializer::<u16>(array.as_any().downcast_ref().unwrap()),
DataType::UInt32 => primitive_serializer::<u32>(array.as_any().downcast_ref().unwrap()),
DataType::UInt64 => primitive_serializer::<u64>(array.as_any().downcast_ref().unwrap()),
DataType::Float32 => primitive_serializer::<f32>(array.as_any().downcast_ref().unwrap()),
DataType::Float64 => primitive_serializer::<f64>(array.as_any().downcast_ref().unwrap()),
DataType::Float32 => f32_serializer(array.as_any().downcast_ref().unwrap()),
DataType::Float64 => f64_serializer(array.as_any().downcast_ref().unwrap()),
DataType::Utf8 => utf8_serializer::<i32>(array.as_any().downcast_ref().unwrap()),
DataType::LargeUtf8 => utf8_serializer::<i64>(array.as_any().downcast_ref().unwrap()),
DataType::Struct(_) => struct_serializer(array.as_any().downcast_ref().unwrap()),
Expand Down
18 changes: 18 additions & 0 deletions tests/it/io/json/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ fn int32() -> Result<()> {
test!(array, expected)
}

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

let expected = r#"[1.5,2.5,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 expected = r#"[1.5,2.5,null,null,5.5]"#;

test!(array, expected)
}

#[test]
fn utf8() -> Result<()> {
let array = Utf8Array::<i32>::from(&vec![Some("a"), Some("b"), Some("c"), Some("d"), None]);
Expand Down