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