cat_gateway/service/common/objects/generic/
json_object.rs1use poem_openapi::{
4 registry::{MetaSchema, MetaSchemaRef},
5 types::{Example, ParseError, ParseFromJSON, ParseResult, ToJSON, Type},
6};
7
8#[derive(Debug, Clone)]
10pub(crate) struct JSONObject(serde_json::Value);
11
12impl Type for JSONObject {
13 type RawElementValueType = Self;
14 type RawValueType = Self;
15
16 const IS_REQUIRED: bool = true;
17
18 fn name() -> std::borrow::Cow<'static, str> {
19 "JSONObject".into()
20 }
21
22 fn schema_ref() -> MetaSchemaRef {
23 MetaSchemaRef::Inline(Box::new(MetaSchema::new("object"))).merge(MetaSchema {
24 description: Some("A JSON object for holding any custom information inside it."),
25 example: Some(Self::example().0),
26 ..poem_openapi::registry::MetaSchema::ANY
27 })
28 }
29
30 fn as_raw_value(&self) -> Option<&Self::RawValueType> {
31 Some(self)
32 }
33
34 fn raw_element_iter<'a>(
35 &'a self,
36 ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
37 Box::new(self.as_raw_value().into_iter())
38 }
39}
40
41impl ParseFromJSON for JSONObject {
42 fn parse_from_json(value: Option<serde_json::Value>) -> ParseResult<Self> {
43 serde_json::Value::parse_from_json(value)
44 .map_err(ParseError::propagate)
45 .map(Self)
46 }
47}
48
49impl ToJSON for JSONObject {
50 fn to_json(&self) -> Option<serde_json::Value> {
51 self.0.to_json()
52 }
53}
54
55impl Example for JSONObject {
56 fn example() -> Self {
57 Self(serde_json::json!({}))
58 }
59}