subgraph/utils/document/json_to_document/
mod.rs1use bson::to_document;
2use log::{debug, error, trace};
3
4use super::DocumentUtils;
5
6impl DocumentUtils {
7 pub fn json_to_document(
8 json_value: &serde_json::Value,
9 ) -> Result<Option<bson::Document>, async_graphql::Error> {
10 debug!("Converting JSON to Document");
11 trace!("JSON Value: {:?}", json_value);
12
13 let document = if json_value.is_array() {
14 match to_document(&json_value[0]) {
15 Ok(value) => Some(value),
16 Err(_) => {
17 error!("Invalid JSON Object - Failed to convert Array to BSON document");
18 return Err(async_graphql::Error::new(
19 "Invalid JSON Object - Failed to convert Array to BSON document",
20 ));
21 }
22 }
23 } else if json_value.is_object() {
24 trace!("JSON Value is Object");
25 match to_document(&json_value) {
26 Ok(value) => Some(value),
27 Err(_) => {
28 error!("Invalid JSON Object - Failed to convert Object to BSON document");
29 return Err(async_graphql::Error::new(
30 "Invalid JSON Object - Failed to convert Object to BSON document",
31 ));
32 }
33 }
34 } else if json_value.is_null() {
35 trace!("JSON Value is Null, returning None");
36 Some(bson::Document::new()) } else {
39 error!("Invalid JSON Object - Received unexpected JSON type");
40 return Err(async_graphql::Error::new(
41 "Invalid JSON Object - Received unexpected JSON type",
42 ));
43 };
44
45 trace!("Converted JSON to Document: {:?}", document);
46
47 Ok(document)
48 }
49}