1use crate::value::Value;
2use ahash::HashMap;
3use serde_json::Number;
4use std::iter::once;
5
6impl From<i64> for Value {
7 fn from(value: i64) -> Self {
8 Value::Number(value.into())
9 }
10}
11
12impl From<i32> for Value {
13 fn from(value: i32) -> Self {
14 Value::Number(value.into())
15 }
16}
17
18impl From<String> for Value {
19 fn from(value: String) -> Self {
20 Value::String(value)
21 }
22}
23
24impl From<&str> for Value {
25 fn from(value: &str) -> Self {
26 Value::String(value.to_string())
27 }
28}
29
30impl From<bool> for Value {
31 fn from(value: bool) -> Self {
32 Value::Boolean(value)
33 }
34}
35
36impl From<f64> for Value {
37 fn from(value: f64) -> Self {
38 Number::from_f64(value).map_or(Value::Null, Value::Number)
39 }
40}
41
42impl From<HashMap<String, Value>> for Value {
43 fn from(value: HashMap<String, Value>) -> Self {
44 Value::Object(value)
45 }
46}
47
48impl From<(String, Value)> for Value {
49 fn from(value: (String, Value)) -> Self {
50 Value::Object(HashMap::from_iter(once(value)))
51 }
52}
53
54impl From<(&str, Value)> for Value {
55 fn from(value: (&str, Value)) -> Self {
56 let (k, v) = value;
57 Value::Object(HashMap::from_iter(once((k.to_string(), v))))
58 }
59}
60
61impl From<Vec<(String, Value)>> for Value {
62 fn from(value: Vec<(String, Value)>) -> Self {
63 Value::Object(HashMap::from_iter(value))
64 }
65}
66
67impl From<Vec<(&str, Value)>> for Value {
68 fn from(value: Vec<(&str, Value)>) -> Self {
69 Value::Object(value.into_iter().map(|(k, v)| (k.to_string(), v)).collect())
70 }
71}
72
73impl From<Vec<Value>> for Value {
74 fn from(value: Vec<Value>) -> Self {
75 Value::Array(value)
76 }
77}
78
79impl From<Number> for Value {
80 fn from(value: Number) -> Self {
81 Value::Number(value)
82 }
83}
84
85impl From<serde_json::Value> for Value {
86 fn from(val: serde_json::Value) -> Self {
87 match val {
88 serde_json::Value::Null => Value::Null,
89 serde_json::Value::Bool(boolean) => Value::Boolean(boolean),
90 serde_json::Value::Number(number) => Value::Number(number),
91 serde_json::Value::String(string) => Value::String(string),
92 serde_json::Value::Array(array) => {
93 Value::array_from_iter(array.into_iter().map(Into::into))
94 }
95 serde_json::Value::Object(object) => {
96 Value::object_from_iter(object.into_iter().map(|(key, value)| (key, value.into())))
97 }
98 }
99 }
100}
101
102impl From<Value> for serde_json::Value {
103 fn from(val: Value) -> Self {
104 match val {
105 Value::Object(object) => {
106 let map = serde_json::Map::from_iter(
107 object.into_iter().map(|(key, value)| (key, value.into())),
108 );
109 serde_json::Value::Object(map)
110 }
111 Value::Array(array) => {
112 serde_json::Value::Array(array.into_iter().map(Into::into).collect())
113 }
114 Value::Boolean(boolean) => serde_json::Value::Bool(boolean),
115 Value::Null => serde_json::Value::Null,
116 Value::String(string) => serde_json::Value::String(string),
117 Value::Number(number) => serde_json::Value::Number(number),
118 }
119 }
120}