Add Bson<->JSON and Document<->JSONObject impls

This commit is contained in:
Samuel Hurel 2021-10-19 10:24:41 +02:00
parent 44812eeb1f
commit 0f997b651d

View File

@ -1,4 +1,5 @@
use bson::oid::ObjectId;
use bson::{oid::ObjectId, Bson, Document};
#[cfg(feature = "chrono")]
use bson::DateTime as UtcDateTime;
#[cfg(feature = "chrono")]
@ -33,3 +34,25 @@ impl ScalarType for UtcDateTime {
self.to_chrono().to_value()
}
}
#[Scalar(internal, name = "JSON")]
impl ScalarType for Bson {
fn parse(value: Value) -> InputValueResult<Self> {
bson::to_bson(&value).map_err(InputValueError::custom)
}
fn to_value(&self) -> Value {
bson::from_bson(self.clone()).unwrap_or_default()
}
}
#[Scalar(internal, name = "JSONObject")]
impl ScalarType for Document {
fn parse(value: Value) -> InputValueResult<Self> {
bson::to_document(&value).map_err(InputValueError::custom)
}
fn to_value(&self) -> Value {
bson::from_document(self.clone()).unwrap_or_default()
}
}