serde_json_bytes/value/
from.rs

1use super::Value;
2use crate::lib::iter::FromIterator;
3use crate::map::Map;
4use crate::{lib::*, ByteString};
5use serde_json::Number;
6
7#[cfg(feature = "arbitrary_precision")]
8use serde::serde_if_integer128;
9
10macro_rules! from_integer {
11    ($($ty:ident)*) => {
12        $(
13            impl From<$ty> for Value {
14                fn from(n: $ty) -> Self {
15                    Value::Number(n.into())
16                }
17            }
18        )*
19    };
20}
21
22from_integer! {
23    i8 i16 i32 i64 isize
24    u8 u16 u32 u64 usize
25}
26
27#[cfg(feature = "arbitrary_precision")]
28serde_if_integer128! {
29    from_integer! {
30        i128 u128
31    }
32}
33
34impl From<f32> for Value {
35    /// Convert 32-bit floating point number to `Value`
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// use serde_json::Value;
41    ///
42    /// let f: f32 = 13.37;
43    /// let x: Value = f.into();
44    /// ```
45    fn from(f: f32) -> Self {
46        From::from(f as f64)
47    }
48}
49
50impl From<f64> for Value {
51    /// Convert 64-bit floating point number to `Value`
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// use serde_json::Value;
57    ///
58    /// let f: f64 = 13.37;
59    /// let x: Value = f.into();
60    /// ```
61    fn from(f: f64) -> Self {
62        Number::from_f64(f).map_or(Value::Null, Value::Number)
63    }
64}
65
66impl From<bool> for Value {
67    /// Convert boolean to `Value`
68    ///
69    /// # Examples
70    ///
71    /// ```
72    /// use serde_json::Value;
73    ///
74    /// let b = false;
75    /// let x: Value = b.into();
76    /// ```
77    fn from(f: bool) -> Self {
78        Value::Bool(f)
79    }
80}
81
82impl From<String> for Value {
83    /// Convert `String` to `Value`
84    ///
85    /// # Examples
86    ///
87    /// ```
88    /// use serde_json::Value;
89    ///
90    /// let s: String = "lorem".to_string();
91    /// let x: Value = s.into();
92    /// ```
93    fn from(f: String) -> Self {
94        Value::String(f.into())
95    }
96}
97
98impl<'a> From<&'a str> for Value {
99    /// Convert string slice to `Value`
100    ///
101    /// # Examples
102    ///
103    /// ```
104    /// use serde_json::Value;
105    ///
106    /// let s: &str = "lorem";
107    /// let x: Value = s.into();
108    /// ```
109    fn from(f: &str) -> Self {
110        Value::String(f.to_string().into())
111    }
112}
113
114impl<'a> From<Cow<'a, str>> for Value {
115    /// Convert copy-on-write string to `Value`
116    ///
117    /// # Examples
118    ///
119    /// ```
120    /// use serde_json::Value;
121    /// use std::borrow::Cow;
122    ///
123    /// let s: Cow<str> = Cow::Borrowed("lorem");
124    /// let x: Value = s.into();
125    /// ```
126    ///
127    /// ```
128    /// use serde_json::Value;
129    /// use std::borrow::Cow;
130    ///
131    /// let s: Cow<str> = Cow::Owned("lorem".to_string());
132    /// let x: Value = s.into();
133    /// ```
134    fn from(f: Cow<'a, str>) -> Self {
135        Value::String(f.into_owned().into())
136    }
137}
138
139impl From<Number> for Value {
140    /// Convert `Number` to `Value`
141    ///
142    /// # Examples
143    ///
144    /// ```
145    /// use serde_json::{Number, Value};
146    ///
147    /// let n = Number::from(7);
148    /// let x: Value = n.into();
149    /// ```
150    fn from(f: Number) -> Self {
151        Value::Number(f)
152    }
153}
154
155impl From<Map<ByteString, Value>> for Value {
156    /// Convert map (with string keys) to `Value`
157    ///
158    /// # Examples
159    ///
160    /// ```
161    /// use serde_json::{Map, Value};
162    ///
163    /// let mut m = Map::new();
164    /// m.insert("Lorem".to_string(), "ipsum".into());
165    /// let x: Value = m.into();
166    /// ```
167    fn from(f: Map<ByteString, Value>) -> Self {
168        Value::Object(f)
169    }
170}
171
172impl<T: Into<Value>> From<Vec<T>> for Value {
173    /// Convert a `Vec` to `Value`
174    ///
175    /// # Examples
176    ///
177    /// ```
178    /// use serde_json::Value;
179    ///
180    /// let v = vec!["lorem", "ipsum", "dolor"];
181    /// let x: Value = v.into();
182    /// ```
183    fn from(f: Vec<T>) -> Self {
184        Value::Array(f.into_iter().map(Into::into).collect())
185    }
186}
187
188impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
189    /// Convert a slice to `Value`
190    ///
191    /// # Examples
192    ///
193    /// ```
194    /// use serde_json::Value;
195    ///
196    /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
197    /// let x: Value = v.into();
198    /// ```
199    fn from(f: &'a [T]) -> Self {
200        Value::Array(f.iter().cloned().map(Into::into).collect())
201    }
202}
203
204impl<T: Into<Value>> FromIterator<T> for Value {
205    /// Convert an iteratable type to a `Value`
206    ///
207    /// # Examples
208    ///
209    /// ```
210    /// use serde_json::Value;
211    ///
212    /// let v = std::iter::repeat(42).take(5);
213    /// let x: Value = v.collect();
214    /// ```
215    ///
216    /// ```
217    /// use serde_json::Value;
218    ///
219    /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
220    /// let x: Value = v.into_iter().collect();
221    /// ```
222    ///
223    /// ```
224    /// use std::iter::FromIterator;
225    /// use serde_json::Value;
226    ///
227    /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
228    /// ```
229    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
230        Value::Array(iter.into_iter().map(Into::into).collect())
231    }
232}
233
234impl<K: Into<ByteString>, V: Into<Value>> FromIterator<(K, V)> for Value {
235    /// Convert an iteratable type to a `Value`
236    ///
237    /// # Examples
238    ///
239    /// ```
240    /// use serde_json::Value;
241    ///
242    /// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)];
243    /// let x: Value = v.into_iter().collect();
244    /// ```
245    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
246        Value::Object(
247            iter.into_iter()
248                .map(|(k, v)| (k.into(), v.into()))
249                .collect(),
250        )
251    }
252}
253
254impl<T: Into<Value>> From<Option<T>> for Value {
255    fn from(opt: Option<T>) -> Self {
256        match opt {
257            Some(value) => value.into(),
258            None => Value::Null,
259        }
260    }
261}
262
263impl From<()> for Value {
264    /// Convert `()` to `Value`
265    ///
266    /// # Examples
267    ///
268    /// ```
269    /// use serde_json::Value;
270    ///
271    /// let u = ();
272    /// let x: Value = u.into();
273    /// ```
274    fn from((): ()) -> Self {
275        Value::Null
276    }
277}