Merge pull request #661 from IcanDivideBy0/bson_json
Add Bson<->JSON and Document<->JSONObject impls
This commit is contained in:
commit
b621cfe420
25
src/types/external/bson.rs
vendored
25
src/types/external/bson.rs
vendored
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user