sea_query/value/
with_json.rs1use super::*;
2
3type_to_box_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_deref(),
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::Json(None) => Json::Null,
37 #[cfg(feature = "with-rust_decimal")]
38 Value::Decimal(None) => Json::Null,
39 #[cfg(feature = "with-bigdecimal")]
40 Value::BigDecimal(None) => Json::Null,
41 #[cfg(feature = "with-uuid")]
42 Value::Uuid(None) => Json::Null,
43 #[cfg(feature = "postgres-array")]
44 Value::Array(_, None) => Json::Null,
45 #[cfg(feature = "postgres-vector")]
46 Value::Vector(None) => Json::Null,
47 #[cfg(feature = "with-ipnetwork")]
48 Value::IpNetwork(None) => Json::Null,
49 #[cfg(feature = "with-mac_address")]
50 Value::MacAddress(None) => Json::Null,
51 #[cfg(feature = "postgres-range")]
52 Value::Range(None) => Json::Null,
53 Value::Bool(Some(b)) => Json::Bool(*b),
54 Value::TinyInt(Some(v)) => (*v).into(),
55 Value::SmallInt(Some(v)) => (*v).into(),
56 Value::Int(Some(v)) => (*v).into(),
57 Value::BigInt(Some(v)) => (*v).into(),
58 Value::TinyUnsigned(Some(v)) => (*v).into(),
59 Value::SmallUnsigned(Some(v)) => (*v).into(),
60 Value::Unsigned(Some(v)) => (*v).into(),
61 Value::BigUnsigned(Some(v)) => (*v).into(),
62 Value::Float(Some(v)) => (*v).into(),
63 Value::Double(Some(v)) => (*v).into(),
64 Value::String(Some(s)) => Json::String(s.clone()),
65 Value::Char(Some(v)) => Json::String(v.to_string()),
66 Value::Bytes(Some(s)) => Json::String(std::str::from_utf8(s).unwrap().to_string()),
67 Value::Json(Some(v)) => v.as_ref().clone(),
68 #[cfg(feature = "with-chrono")]
69 Value::ChronoDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
70 #[cfg(feature = "with-chrono")]
71 Value::ChronoTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
72 #[cfg(feature = "with-chrono")]
73 Value::ChronoDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
74 #[cfg(feature = "with-chrono")]
75 Value::ChronoDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
76 #[cfg(feature = "with-chrono")]
77 Value::ChronoDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
78 #[cfg(feature = "with-chrono")]
79 Value::ChronoDateTimeLocal(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
80 #[cfg(feature = "with-time")]
81 Value::TimeDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
82 #[cfg(feature = "with-time")]
83 Value::TimeTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
84 #[cfg(feature = "with-time")]
85 Value::TimeDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
86 #[cfg(feature = "with-time")]
87 Value::TimeDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
88 #[cfg(feature = "with-jiff")]
89 Value::JiffDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
90 #[cfg(feature = "with-jiff")]
91 Value::JiffTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
92 #[cfg(feature = "with-jiff")]
93 Value::JiffDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
94 #[cfg(feature = "with-jiff")]
95 Value::JiffTimestamp(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
96 #[cfg(feature = "with-jiff")]
97 Value::JiffZoned(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
98 #[cfg(feature = "with-rust_decimal")]
99 Value::Decimal(Some(v)) => {
100 use rust_decimal::prelude::ToPrimitive;
101 v.to_f64().unwrap().into()
102 }
103 #[cfg(feature = "with-bigdecimal")]
104 Value::BigDecimal(Some(v)) => {
105 use bigdecimal::ToPrimitive;
106 v.to_f64().unwrap().into()
107 }
108 #[cfg(feature = "with-uuid")]
109 Value::Uuid(Some(v)) => Json::String(v.to_string()),
110 #[cfg(feature = "postgres-array")]
111 Value::Array(_, Some(v)) => Json::Array(v.iter().map(sea_value_to_json_value).collect()),
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}