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