sea_query/value/
with_json.rs1use super::*;
2
3type_to_value!(Json, Json, Json);
4
5impl Value {
6 pub fn is_json(&self) -> bool {
7 matches!(self, Self::Json(_))
8 }
9
10 pub fn as_ref_json(&self) -> Option<&Json> {
11 match self {
12 Self::Json(v) => v.as_ref(),
13 _ => panic!("not Value::Json"),
14 }
15 }
16}
17
18#[allow(clippy::many_single_char_names)]
20pub fn sea_value_to_json_value(value: &Value) -> Json {
21 match value {
22 Value::Bool(None)
23 | Value::TinyInt(None)
24 | Value::SmallInt(None)
25 | Value::Int(None)
26 | Value::BigInt(None)
27 | Value::TinyUnsigned(None)
28 | Value::SmallUnsigned(None)
29 | Value::Unsigned(None)
30 | Value::BigUnsigned(None)
31 | Value::Float(None)
32 | Value::Double(None)
33 | Value::String(None)
34 | Value::Char(None)
35 | Value::Bytes(None)
36 | Value::Enum(None)
37 | Value::Json(None) => Json::Null,
38 #[cfg(feature = "with-rust_decimal")]
39 Value::Decimal(None) => Json::Null,
40 #[cfg(feature = "with-bigdecimal")]
41 Value::BigDecimal(None) => Json::Null,
42 #[cfg(feature = "with-uuid")]
43 Value::Uuid(None) => Json::Null,
44 #[cfg(feature = "postgres-vector")]
45 Value::Vector(None) => Json::Null,
46 #[cfg(feature = "with-ipnetwork")]
47 Value::IpNetwork(None) => Json::Null,
48 #[cfg(feature = "with-mac_address")]
49 Value::MacAddress(None) => Json::Null,
50 #[cfg(feature = "postgres-range")]
51 Value::Range(None) => Json::Null,
52 Value::Bool(Some(b)) => Json::Bool(*b),
53 Value::TinyInt(Some(v)) => (*v).into(),
54 Value::SmallInt(Some(v)) => (*v).into(),
55 Value::Int(Some(v)) => (*v).into(),
56 Value::BigInt(Some(v)) => (*v).into(),
57 Value::TinyUnsigned(Some(v)) => (*v).into(),
58 Value::SmallUnsigned(Some(v)) => (*v).into(),
59 Value::Unsigned(Some(v)) => (*v).into(),
60 Value::BigUnsigned(Some(v)) => (*v).into(),
61 Value::Float(Some(v)) => (*v).into(),
62 Value::Double(Some(v)) => (*v).into(),
63 Value::String(Some(s)) => Json::String(s.clone()),
64 Value::Char(Some(v)) => Json::String(v.to_string()),
65 Value::Bytes(Some(s)) => Json::String(std::str::from_utf8(s).unwrap().to_string()),
66 Value::Json(Some(v)) => v.clone(),
67 #[cfg(feature = "with-chrono")]
68 Value::ChronoDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
69 #[cfg(feature = "with-chrono")]
70 Value::ChronoTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
71 #[cfg(feature = "with-chrono")]
72 Value::ChronoDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
73 #[cfg(feature = "with-chrono")]
74 Value::ChronoDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
75 #[cfg(feature = "with-chrono")]
76 Value::ChronoDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
77 #[cfg(feature = "with-chrono")]
78 Value::ChronoDateTimeLocal(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
79 #[cfg(feature = "with-time")]
80 Value::TimeDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
81 #[cfg(feature = "with-time")]
82 Value::TimeTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
83 #[cfg(feature = "with-time")]
84 Value::TimeDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
85 #[cfg(feature = "with-time")]
86 Value::TimeDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
87 #[cfg(feature = "with-jiff")]
88 Value::JiffDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
89 #[cfg(feature = "with-jiff")]
90 Value::JiffTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
91 #[cfg(feature = "with-jiff")]
92 Value::JiffDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
93 #[cfg(feature = "with-jiff")]
94 Value::JiffTimestamp(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
95 #[cfg(feature = "with-jiff")]
96 Value::JiffZoned(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
97 #[cfg(feature = "with-rust_decimal")]
98 Value::Decimal(Some(v)) => {
99 use rust_decimal::prelude::ToPrimitive;
100 v.to_f64().unwrap().into()
101 }
102 #[cfg(feature = "with-bigdecimal")]
103 Value::BigDecimal(Some(v)) => {
104 use bigdecimal::ToPrimitive;
105 v.to_f64().unwrap().into()
106 }
107 #[cfg(feature = "with-uuid")]
108 Value::Uuid(Some(v)) => Json::String(v.to_string()),
109 Value::Enum(Some(v)) => Json::String(v.value.to_string()),
110 #[cfg(feature = "postgres-array")]
111 Value::Array(v) => v.to_json_value(),
112 #[cfg(feature = "postgres-vector")]
113 Value::Vector(Some(v)) => Json::Array(v.as_slice().iter().map(|&v| v.into()).collect()),
114 #[cfg(feature = "with-ipnetwork")]
115 Value::IpNetwork(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
116 #[cfg(feature = "with-mac_address")]
117 Value::MacAddress(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
118 #[cfg(feature = "postgres-range")]
119 Value::Range(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
120 }
121}