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

Commit

Permalink
generic float serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSchneider committed May 16, 2022
1 parent 2812f28 commit 19968a3
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions src/io/json/write/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,17 @@ fn primitive_serializer<'a, T: NativeType + ToLexical>(
))
}

fn f64_serializer<'a>(
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> {
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 f32::is_nan(*x) {
if T::is_nan(*x) {
buf.extend(b"null")
} else {
lexical_to_bytes_mut(*x, buf)
Expand Down Expand Up @@ -236,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 => f32_serializer(array.as_any().downcast_ref().unwrap()),
DataType::Float64 => f64_serializer(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

0 comments on commit 19968a3

Please sign in to comment.