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 all commits
Commits
File filter

Filter by extension

Filter by extension

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

fn float_serializer<'a, T>(
array: &'a PrimitiveArray<T>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync>
where
T: num_traits::Float + NativeType + 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![],
))
}

fn utf8_serializer<'a, O: Offset>(
array: &'a Utf8Array<O>,
) -> Box<dyn StreamingIterator<Item = [u8]> + 'a + Send + Sync> {
Expand Down Expand Up @@ -196,8 +219,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 => float_serializer::<f32>(array.as_any().downcast_ref().unwrap()),
DataType::Float64 => float_serializer::<f64>(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
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