Skip to main content

sea_query/value/
with_json.rs

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