polars_core/serde/
series.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use polars_utils::pl_serialize::deserialize_map_bytes;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::prelude::*;

impl Series {
    pub fn serialize_into_writer(&self, writer: &mut dyn std::io::Write) -> PolarsResult<()> {
        let mut df =
            unsafe { DataFrame::new_no_checks_height_from_first(vec![self.clone().into_column()]) };

        df.serialize_into_writer(writer)
    }

    pub fn serialize_to_bytes(&self) -> PolarsResult<Vec<u8>> {
        let mut buf = vec![];
        self.serialize_into_writer(&mut buf)?;

        Ok(buf)
    }

    pub fn deserialize_from_reader(reader: &mut dyn std::io::Read) -> PolarsResult<Self> {
        let df = DataFrame::deserialize_from_reader(reader)?;

        if df.width() != 1 {
            polars_bail!(
                ShapeMismatch:
                "expected only 1 column when deserializing Series from IPC, got columns: {:?}",
                df.schema().iter_names().collect::<Vec<_>>()
            )
        }

        Ok(df.take_columns().swap_remove(0).take_materialized_series())
    }
}

impl Serialize for Series {
    fn serialize<S>(
        &self,
        serializer: S,
    ) -> std::result::Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        use serde::ser::Error;

        serializer.serialize_bytes(
            self.serialize_to_bytes()
                .map_err(S::Error::custom)?
                .as_slice(),
        )
    }
}

impl<'de> Deserialize<'de> for Series {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, <D as Deserializer<'de>>::Error>
    where
        D: Deserializer<'de>,
    {
        deserialize_map_bytes(deserializer, &mut |b| {
            let v = &mut b.as_ref();
            Self::deserialize_from_reader(v)
        })?
        .map_err(D::Error::custom)
    }
}