]>
git.proxmox.com Git - cargo.git/blob - vendor/serde_json/src/value/from.rs
7 macro_rules
! from_integer
{
10 impl From
<$ty
> for Value
{
11 fn from(n
: $ty
) -> Self {
12 Value
::Number(n
.into())
24 #[cfg(feature = "arbitrary_precision")]
25 serde_if_integer128
! {
31 impl From
<f32> for Value
{
32 /// Convert 32-bit floating point number to `Value`
37 /// use serde_json::Value;
39 /// let f: f32 = 13.37;
40 /// let x: Value = f.into();
42 fn from(f
: f32) -> Self {
47 impl From
<f64> for Value
{
48 /// Convert 64-bit floating point number to `Value`
53 /// use serde_json::Value;
55 /// let f: f64 = 13.37;
56 /// let x: Value = f.into();
58 fn from(f
: f64) -> Self {
59 Number
::from_f64(f
).map_or(Value
::Null
, Value
::Number
)
63 impl From
<bool
> for Value
{
64 /// Convert boolean to `Value`
69 /// use serde_json::Value;
72 /// let x: Value = b.into();
74 fn from(f
: bool
) -> Self {
79 impl From
<String
> for Value
{
80 /// Convert `String` to `Value`
85 /// use serde_json::Value;
87 /// let s: String = "lorem".to_string();
88 /// let x: Value = s.into();
90 fn from(f
: String
) -> Self {
95 impl<'a
> From
<&'a
str> for Value
{
96 /// Convert string slice to `Value`
101 /// use serde_json::Value;
103 /// let s: &str = "lorem";
104 /// let x: Value = s.into();
106 fn from(f
: &str) -> Self {
107 Value
::String(f
.to_string())
111 impl<'a
> From
<Cow
<'a
, str>> for Value
{
112 /// Convert copy-on-write string to `Value`
117 /// use serde_json::Value;
118 /// use std::borrow::Cow;
120 /// let s: Cow<str> = Cow::Borrowed("lorem");
121 /// let x: Value = s.into();
125 /// use serde_json::Value;
126 /// use std::borrow::Cow;
128 /// let s: Cow<str> = Cow::Owned("lorem".to_string());
129 /// let x: Value = s.into();
131 fn from(f
: Cow
<'a
, str>) -> Self {
132 Value
::String(f
.into_owned())
136 impl From
<Map
<String
, Value
>> for Value
{
137 /// Convert map (with string keys) to `Value`
142 /// use serde_json::{Map, Value};
144 /// let mut m = Map::new();
145 /// m.insert("Lorem".to_string(), "ipsum".into());
146 /// let x: Value = m.into();
148 fn from(f
: Map
<String
, Value
>) -> Self {
153 impl<T
: Into
<Value
>> From
<Vec
<T
>> for Value
{
154 /// Convert a `Vec` to `Value`
159 /// use serde_json::Value;
161 /// let v = vec!["lorem", "ipsum", "dolor"];
162 /// let x: Value = v.into();
164 fn from(f
: Vec
<T
>) -> Self {
165 Value
::Array(f
.into_iter().map(Into
::into
).collect())
169 impl<'a
, T
: Clone
+ Into
<Value
>> From
<&'a
[T
]> for Value
{
170 /// Convert a slice to `Value`
175 /// use serde_json::Value;
177 /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
178 /// let x: Value = v.into();
180 fn from(f
: &'a
[T
]) -> Self {
181 Value
::Array(f
.iter().cloned().map(Into
::into
).collect())
185 impl<T
: Into
<Value
>> ::std
::iter
::FromIterator
<T
> for Value
{
186 /// Convert an iteratable type to a `Value`
191 /// use serde_json::Value;
193 /// let v = std::iter::repeat(42).take(5);
194 /// let x: Value = v.collect();
198 /// use serde_json::Value;
200 /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
201 /// let x: Value = v.into_iter().collect();
205 /// use std::iter::FromIterator;
206 /// use serde_json::Value;
208 /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
210 fn from_iter
<I
: IntoIterator
<Item
= T
>>(iter
: I
) -> Self {
211 Value
::Array(iter
.into_iter().map(Into
::into
).collect())