]> git.proxmox.com Git - cargo.git/blob - vendor/serde_json/src/value/from.rs
New upstream version 0.33.0
[cargo.git] / vendor / serde_json / src / value / from.rs
1 use std::borrow::Cow;
2
3 use super::Value;
4 use map::Map;
5 use number::Number;
6
7 macro_rules! from_integer {
8 ($($ty:ident)*) => {
9 $(
10 impl From<$ty> for Value {
11 fn from(n: $ty) -> Self {
12 Value::Number(n.into())
13 }
14 }
15 )*
16 };
17 }
18
19 from_integer! {
20 i8 i16 i32 i64 isize
21 u8 u16 u32 u64 usize
22 }
23
24 #[cfg(feature = "arbitrary_precision")]
25 serde_if_integer128! {
26 from_integer! {
27 i128 u128
28 }
29 }
30
31 impl From<f32> for Value {
32 /// Convert 32-bit floating point number to `Value`
33 ///
34 /// # Examples
35 ///
36 /// ```edition2018
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 From::from(f as f64)
44 }
45 }
46
47 impl From<f64> for Value {
48 /// Convert 64-bit floating point number to `Value`
49 ///
50 /// # Examples
51 ///
52 /// ```edition2018
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(Value::Null, Value::Number)
60 }
61 }
62
63 impl From<bool> for Value {
64 /// Convert boolean to `Value`
65 ///
66 /// # Examples
67 ///
68 /// ```edition2018
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 /// ```edition2018
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 /// ```edition2018
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 /// ```edition2018
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 /// ```edition2018
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<Map<String, Value>> for Value {
137 /// Convert map (with string keys) to `Value`
138 ///
139 /// # Examples
140 ///
141 /// ```edition2018
142 /// use serde_json::{Map, Value};
143 ///
144 /// let mut m = Map::new();
145 /// m.insert("Lorem".to_string(), "ipsum".into());
146 /// let x: Value = m.into();
147 /// ```
148 fn from(f: Map<String, Value>) -> Self {
149 Value::Object(f)
150 }
151 }
152
153 impl<T: Into<Value>> From<Vec<T>> for Value {
154 /// Convert a `Vec` to `Value`
155 ///
156 /// # Examples
157 ///
158 /// ```edition2018
159 /// use serde_json::Value;
160 ///
161 /// let v = vec!["lorem", "ipsum", "dolor"];
162 /// let x: Value = v.into();
163 /// ```
164 fn from(f: Vec<T>) -> Self {
165 Value::Array(f.into_iter().map(Into::into).collect())
166 }
167 }
168
169 impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
170 /// Convert a slice to `Value`
171 ///
172 /// # Examples
173 ///
174 /// ```edition2018
175 /// use serde_json::Value;
176 ///
177 /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
178 /// let x: Value = v.into();
179 /// ```
180 fn from(f: &'a [T]) -> Self {
181 Value::Array(f.iter().cloned().map(Into::into).collect())
182 }
183 }
184
185 impl<T: Into<Value>> ::std::iter::FromIterator<T> for Value {
186 /// Convert an iteratable type to a `Value`
187 ///
188 /// # Examples
189 ///
190 /// ```edition2018
191 /// use serde_json::Value;
192 ///
193 /// let v = std::iter::repeat(42).take(5);
194 /// let x: Value = v.collect();
195 /// ```
196 ///
197 /// ```edition2018
198 /// use serde_json::Value;
199 ///
200 /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
201 /// let x: Value = v.into_iter().collect();
202 /// ```
203 ///
204 /// ```edition2018
205 /// use std::iter::FromIterator;
206 /// use serde_json::Value;
207 ///
208 /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
209 /// ```
210 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
211 Value::Array(iter.into_iter().map(Into::into).collect())
212 }
213 }