Merge pull request #661 from IcanDivideBy0/bson_json

Add Bson<->JSON and Document<->JSONObject impls
This commit is contained in:
Sunli 2021-10-19 18:42:22 +08:00 committed by GitHub
commit b621cfe420

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()
}
}