qdrant_client/
serde.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![allow(deprecated)]

use std::collections::HashMap;
use std::fmt::{Display, Formatter};

use serde::ser::{SerializeMap, SerializeSeq};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::qdrant::value::Kind;
use crate::qdrant::{ListValue, Struct, Value};

#[derive(Debug)]
#[deprecated(
    since = "1.10.0",
    note = "use `qdrant_client::Error::JsonToPayload` error variant instead"
)]
#[allow(dead_code)]
pub struct PayloadConversionError(serde_json::Value);

impl Display for PayloadConversionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Failed to convert json {} to payload: expected object at the top level",
            self.0
        )
    }
}

impl Serialize for Value {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if let Some(value) = &self.kind {
            match value {
                Kind::NullValue(_v) => serializer.serialize_none(),
                Kind::DoubleValue(double) => serializer.serialize_f64(*double),
                Kind::IntegerValue(int) => serializer.serialize_i64(*int),
                Kind::StringValue(str) => serializer.serialize_str(str),
                Kind::BoolValue(bool) => serializer.serialize_bool(*bool),
                Kind::StructValue(struc) => {
                    let mut map = serializer.serialize_map(Some(struc.fields.len()))?;
                    for (k, v) in struc.fields.iter() {
                        map.serialize_entry(k, v)?;
                    }
                    map.end()
                }
                Kind::ListValue(list) => {
                    let mut seq = serializer.serialize_seq(Some(list.values.len()))?;
                    for v in &list.values {
                        seq.serialize_element(v)?;
                    }
                    seq.end()
                }
            }
        } else {
            serializer.serialize_none()
        }
    }
}

impl From<serde_json::Value> for Value {
    fn from(value: serde_json::Value) -> Self {
        match value {
            serde_json::Value::Null => Self {
                kind: Some(Kind::NullValue(0)),
            },
            serde_json::Value::Bool(bool) => Self {
                kind: Some(Kind::BoolValue(bool)),
            },
            serde_json::Value::Number(num) => {
                if let Some(num) = num.as_i64() {
                    Self {
                        kind: Some(Kind::IntegerValue(num)),
                    }
                } else if let Some(num) = num.as_f64() {
                    Self {
                        kind: Some(Kind::DoubleValue(num)),
                    }
                } else {
                    Self {
                        kind: Some(Kind::DoubleValue(f64::NAN)),
                    }
                }
            }
            serde_json::Value::String(str) => Self {
                kind: Some(Kind::StringValue(str)),
            },
            serde_json::Value::Array(arr) => Self {
                kind: Some(Kind::ListValue(ListValue {
                    values: arr.into_iter().map(|v| v.into()).collect(),
                })),
            },
            serde_json::Value::Object(obj) => Self {
                kind: Some(Kind::StructValue(Struct {
                    fields: obj
                        .into_iter()
                        .map(|(k, v)| (k, v.into()))
                        .collect::<HashMap<_, _>>(),
                })),
            },
        }
    }
}

impl<'de> Deserialize<'de> for Value {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        // rely on serde_json to materialize a JSON value for conversion
        let serde_value = serde_json::Value::deserialize(deserializer)?;
        Ok(serde_value.into())
    }
}