• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use super::Value;
2 use crate::lib::iter::FromIterator;
3 use crate::lib::*;
4 use crate::map::Map;
5 use crate::number::Number;
6 
7 #[cfg(feature = "arbitrary_precision")]
8 use serde::serde_if_integer128;
9 
10 macro_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 
22 from_integer! {
23     i8 i16 i32 i64 isize
24     u8 u16 u32 u64 usize
25 }
26 
27 #[cfg(feature = "arbitrary_precision")]
28 serde_if_integer128! {
29     from_integer! {
30         i128 u128
31     }
32 }
33 
34 impl 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     /// ```
from(f: f32) -> Self45     fn from(f: f32) -> Self {
46         From::from(f as f64)
47     }
48 }
49 
50 impl 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     /// ```
from(f: f64) -> Self61     fn from(f: f64) -> Self {
62         Number::from_f64(f).map_or(Value::Null, Value::Number)
63     }
64 }
65 
66 impl 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     /// ```
from(f: bool) -> Self77     fn from(f: bool) -> Self {
78         Value::Bool(f)
79     }
80 }
81 
82 impl 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     /// ```
from(f: String) -> Self93     fn from(f: String) -> Self {
94         Value::String(f)
95     }
96 }
97 
98 impl<'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     /// ```
from(f: &str) -> Self109     fn from(f: &str) -> Self {
110         Value::String(f.to_string())
111     }
112 }
113 
114 impl<'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     /// ```
from(f: Cow<'a, str>) -> Self134     fn from(f: Cow<'a, str>) -> Self {
135         Value::String(f.into_owned())
136     }
137 }
138 
139 impl 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     /// ```
from(f: Number) -> Self150     fn from(f: Number) -> Self {
151         Value::Number(f)
152     }
153 }
154 
155 impl From<Map<String, 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     /// ```
from(f: Map<String, Value>) -> Self167     fn from(f: Map<String, Value>) -> Self {
168         Value::Object(f)
169     }
170 }
171 
172 impl<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     /// ```
from(f: Vec<T>) -> Self183     fn from(f: Vec<T>) -> Self {
184         Value::Array(f.into_iter().map(Into::into).collect())
185     }
186 }
187 
188 impl<'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     /// ```
from(f: &'a [T]) -> Self199     fn from(f: &'a [T]) -> Self {
200         Value::Array(f.iter().cloned().map(Into::into).collect())
201     }
202 }
203 
204 impl<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     /// ```
from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self229     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
230         Value::Array(iter.into_iter().map(Into::into).collect())
231     }
232 }
233 
234 impl<K: Into<String>, 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     /// ```
from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self245     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 
254 impl From<()> for Value {
255     /// Convert `()` to `Value`
256     ///
257     /// # Examples
258     ///
259     /// ```
260     /// use serde_json::Value;
261     ///
262     /// let u = ();
263     /// let x: Value = u.into();
264     /// ```
from((): ()) -> Self265     fn from((): ()) -> Self {
266         Value::Null
267     }
268 }
269