From 5f3dbda31025333afead06b28ecb555e6110de70 Mon Sep 17 00:00:00 2001 From: Samuel Hurel Date: Tue, 19 Oct 2021 10:24:41 +0200 Subject: [PATCH] Add Bson<->JSON and Document<->JSONObject impls --- src/types/external/bson.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/types/external/bson.rs b/src/types/external/bson.rs index 228cf52c..2686d049 100644 --- a/src/types/external/bson.rs +++ b/src/types/external/bson.rs @@ -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 { + 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 { + bson::to_document(&value).map_err(InputValueError::custom) + } + + fn to_value(&self) -> Value { + bson::from_document(self.clone()).unwrap_or_default() + } +}