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::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 Value::Bool(Some(b)) => Json::Bool(*b),
52 Value::TinyInt(Some(v)) => (*v).into(),
53 Value::SmallInt(Some(v)) => (*v).into(),
54 Value::Int(Some(v)) => (*v).into(),
55 Value::BigInt(Some(v)) => (*v).into(),
56 Value::TinyUnsigned(Some(v)) => (*v).into(),
57 Value::SmallUnsigned(Some(v)) => (*v).into(),
58 Value::Unsigned(Some(v)) => (*v).into(),
59 Value::BigUnsigned(Some(v)) => (*v).into(),
60 Value::Float(Some(v)) => (*v).into(),
61 Value::Double(Some(v)) => (*v).into(),
62 Value::String(Some(s)) => Json::String(s.clone()),
63 Value::Char(Some(v)) => Json::String(v.to_string()),
64 Value::Bytes(Some(s)) => Json::String(from_utf8(s).unwrap().to_string()),
65 Value::Json(Some(v)) => v.clone(),
66 #[cfg(feature = "with-chrono")]
67 Value::ChronoDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
68 #[cfg(feature = "with-chrono")]
69 Value::ChronoTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
70 #[cfg(feature = "with-chrono")]
71 Value::ChronoDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
72 #[cfg(feature = "with-chrono")]
73 Value::ChronoDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
74 #[cfg(feature = "with-chrono")]
75 Value::ChronoDateTimeUtc(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
76 #[cfg(feature = "with-chrono")]
77 Value::ChronoDateTimeLocal(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
78 #[cfg(feature = "with-time")]
79 Value::TimeDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
80 #[cfg(feature = "with-time")]
81 Value::TimeTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
82 #[cfg(feature = "with-time")]
83 Value::TimeDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
84 #[cfg(feature = "with-time")]
85 Value::TimeDateTimeWithTimeZone(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
86 #[cfg(feature = "with-jiff")]
87 Value::JiffDate(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
88 #[cfg(feature = "with-jiff")]
89 Value::JiffTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
90 #[cfg(feature = "with-jiff")]
91 Value::JiffDateTime(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
92 #[cfg(feature = "with-jiff")]
93 Value::JiffTimestamp(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
94 #[cfg(feature = "with-jiff")]
95 Value::JiffZoned(_) => CommonSqlQueryBuilder.value_to_string(value).into(),
96 #[cfg(feature = "with-rust_decimal")]
97 Value::Decimal(Some(v)) => {
98 use rust_decimal::prelude::ToPrimitive;
99 v.to_f64().unwrap().into()
100 }
101 #[cfg(feature = "with-bigdecimal")]
102 Value::BigDecimal(Some(v)) => {
103 use bigdecimal::ToPrimitive;
104 v.to_f64().unwrap().into()
105 }
106 #[cfg(feature = "with-uuid")]
107 Value::Uuid(Some(v)) => Json::String(v.to_string()),
108 #[cfg(feature = "postgres-array")]
109 Value::Array(_, Some(v)) => Json::Array(v.iter().map(sea_value_to_json_value).collect()),
110 #[cfg(feature = "postgres-vector")]
111 Value::Vector(Some(v)) => Json::Array(v.as_slice().iter().map(|&v| v.into()).collect()),
112 #[cfg(feature = "with-ipnetwork")]
113 Value::IpNetwork(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
114 #[cfg(feature = "with-mac_address")]
115 Value::MacAddress(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
116 }
117}