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)
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(Some(_))
72 | Value::ChronoTime(Some(_))
73 | Value::ChronoDateTime(Some(_))
74 | Value::ChronoDateTimeWithTimeZone(Some(_))
75 | Value::ChronoDateTimeUtc(Some(_))
76 | Value::ChronoDateTimeLocal(Some(_)) => {
77 CommonSqlQueryBuilder.value_to_string(value).into()
78 }
79 #[cfg(feature = "with-chrono")]
80 Value::ChronoDate(None)
81 | Value::ChronoTime(None)
82 | Value::ChronoDateTime(None)
83 | Value::ChronoDateTimeWithTimeZone(None)
84 | Value::ChronoDateTimeUtc(None)
85 | Value::ChronoDateTimeLocal(None) => Json::Null,
86 #[cfg(feature = "with-time")]
87 Value::TimeDate(Some(_))
88 | Value::TimeTime(Some(_))
89 | Value::TimeDateTime(Some(_))
90 | Value::TimeDateTimeWithTimeZone(Some(_)) => {
91 CommonSqlQueryBuilder.value_to_string(value).into()
92 }
93 #[cfg(feature = "with-time")]
94 Value::TimeDate(None)
95 | Value::TimeTime(None)
96 | Value::TimeDateTime(None)
97 | Value::TimeDateTimeWithTimeZone(None) => Json::Null,
98 #[cfg(feature = "with-jiff")]
99 Value::JiffDate(Some(_))
100 | Value::JiffTime(Some(_))
101 | Value::JiffDateTime(Some(_))
102 | Value::JiffTimestamp(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
103 #[cfg(feature = "with-jiff")]
104 Value::JiffDate(None)
105 | Value::JiffTime(None)
106 | Value::JiffDateTime(None)
107 | Value::JiffTimestamp(None) => Json::Null,
108 #[cfg(feature = "with-rust_decimal")]
109 Value::Decimal(Some(v)) => {
110 use rust_decimal::prelude::ToPrimitive;
111 v.to_f64().unwrap().into()
112 }
113 #[cfg(feature = "with-bigdecimal")]
114 Value::BigDecimal(Some(v)) => {
115 use bigdecimal::ToPrimitive;
116 v.to_f64().unwrap().into()
117 }
118 #[cfg(feature = "with-uuid")]
119 Value::Uuid(Some(v)) => Json::String(v.to_string()),
120 #[cfg(feature = "postgres-array")]
121 Value::Array(_, Some(v)) => Json::Array(v.iter().map(sea_value_to_json_value).collect()),
122 #[cfg(feature = "postgres-vector")]
123 Value::Vector(Some(v)) => Json::Array(v.as_slice().iter().map(|&v| v.into()).collect()),
124 #[cfg(feature = "with-ipnetwork")]
125 Value::IpNetwork(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
126 #[cfg(feature = "with-mac_address")]
127 Value::MacAddress(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
128 #[cfg(feature = "postgres-range")]
129 Value::Range(Some(_)) => CommonSqlQueryBuilder.value_to_string(value).into(),
130 }
131}