Skip to main content

bison_db/
serde_support.rs

1//! `serde` integration for [`Value`] and [`Document`], behind the `serde` feature.
2//!
3//! [`Value`] maps onto the serde data model the same way a dynamic JSON value
4//! does: each variant serialises as its natural counterpart, and deserialising
5//! reconstructs the closest variant from whatever the format produced. This is
6//! what lets a caller move documents in and out of JSON, MessagePack, or any
7//! other `serde` format without a bespoke conversion layer.
8//!
9//! [`Document`] serialises as a map and deserialises from one, preserving field
10//! order as the underlying format allows.
11
12use alloc::string::String;
13use alloc::vec::Vec;
14use core::fmt;
15
16use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
17use serde::ser::{Serialize, SerializeMap, SerializeSeq, Serializer};
18
19use crate::value::{Document, Value};
20
21impl Serialize for Value {
22    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
23        match self {
24            Value::Null => serializer.serialize_unit(),
25            Value::Bool(b) => serializer.serialize_bool(*b),
26            Value::Int(n) => serializer.serialize_i64(*n),
27            Value::Float(f) => serializer.serialize_f64(*f),
28            Value::Str(s) => serializer.serialize_str(s),
29            Value::Bytes(b) => serializer.serialize_bytes(b),
30            Value::Array(items) => {
31                let mut seq = serializer.serialize_seq(Some(items.len()))?;
32                for item in items {
33                    seq.serialize_element(item)?;
34                }
35                seq.end()
36            }
37            Value::Object(doc) => doc.serialize(serializer),
38        }
39    }
40}
41
42impl Serialize for Document {
43    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
44        let mut map = serializer.serialize_map(Some(self.len()))?;
45        for (key, value) in self {
46            map.serialize_entry(key, value)?;
47        }
48        map.end()
49    }
50}
51
52/// Visitor that turns any serde value into the closest [`Value`] variant.
53struct ValueVisitor;
54
55impl<'de> Visitor<'de> for ValueVisitor {
56    type Value = Value;
57
58    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.write_str("any bison-db value")
60    }
61
62    fn visit_bool<E>(self, v: bool) -> Result<Value, E> {
63        Ok(Value::Bool(v))
64    }
65
66    fn visit_i64<E>(self, v: i64) -> Result<Value, E> {
67        Ok(Value::Int(v))
68    }
69
70    fn visit_u64<E: de::Error>(self, v: u64) -> Result<Value, E> {
71        i64::try_from(v)
72            .map(Value::Int)
73            .map_err(|_| E::custom("integer out of i64 range"))
74    }
75
76    fn visit_f64<E>(self, v: f64) -> Result<Value, E> {
77        Ok(Value::Float(v))
78    }
79
80    fn visit_str<E>(self, v: &str) -> Result<Value, E> {
81        Ok(Value::Str(String::from(v)))
82    }
83
84    fn visit_string<E>(self, v: String) -> Result<Value, E> {
85        Ok(Value::Str(v))
86    }
87
88    fn visit_bytes<E>(self, v: &[u8]) -> Result<Value, E> {
89        Ok(Value::Bytes(v.to_vec()))
90    }
91
92    fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Value, E> {
93        Ok(Value::Bytes(v))
94    }
95
96    fn visit_none<E>(self) -> Result<Value, E> {
97        Ok(Value::Null)
98    }
99
100    fn visit_unit<E>(self) -> Result<Value, E> {
101        Ok(Value::Null)
102    }
103
104    fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Value, D::Error> {
105        Value::deserialize(deserializer)
106    }
107
108    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Value, A::Error> {
109        let mut items = Vec::with_capacity(seq.size_hint().unwrap_or(0));
110        while let Some(item) = seq.next_element()? {
111            items.push(item);
112        }
113        Ok(Value::Array(items))
114    }
115
116    fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Value, A::Error> {
117        Ok(Value::Object(document_from_map(map)?))
118    }
119}
120
121impl<'de> Deserialize<'de> for Value {
122    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
123        deserializer.deserialize_any(ValueVisitor)
124    }
125}
126
127/// Visitor that builds a [`Document`] from a serde map.
128struct DocumentVisitor;
129
130impl<'de> Visitor<'de> for DocumentVisitor {
131    type Value = Document;
132
133    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134        f.write_str("a map of document fields")
135    }
136
137    fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Document, A::Error> {
138        document_from_map(map)
139    }
140}
141
142/// Drains a [`MapAccess`] into a [`Document`], used by both visitors above.
143fn document_from_map<'de, A: MapAccess<'de>>(mut map: A) -> Result<Document, A::Error> {
144    let mut doc = Document::with_capacity(map.size_hint().unwrap_or(0));
145    while let Some((key, value)) = map.next_entry::<String, Value>()? {
146        let _ = doc.set(key, value);
147    }
148    Ok(doc)
149}
150
151impl<'de> Deserialize<'de> for Document {
152    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
153        deserializer.deserialize_map(DocumentVisitor)
154    }
155}
156
157#[cfg(all(test, feature = "std"))]
158#[allow(clippy::unwrap_used, clippy::expect_used)]
159mod tests {
160    use crate::{Document, Value};
161
162    #[test]
163    fn test_value_json_roundtrip() {
164        let mut doc = Document::new();
165        doc.set("name", "ada")
166            .set("age", 36_i64)
167            .set("active", true);
168        let json = serde_json::to_string(&doc).unwrap();
169        let back: Document = serde_json::from_str(&json).unwrap();
170        assert_eq!(doc, back);
171    }
172
173    #[test]
174    fn test_nested_value_roundtrip() {
175        let v = Value::Array(vec![
176            Value::from(1_i64),
177            Value::from("two"),
178            Value::Null,
179            Value::from(3.5_f64),
180        ]);
181        let json = serde_json::to_string(&v).unwrap();
182        let back: Value = serde_json::from_str(&json).unwrap();
183        assert_eq!(v, back);
184    }
185}