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