/* Copyright 2010-present MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using MongoDB.Bson.IO; namespace MongoDB.Bson.Serialization { /// /// Extensions methods for IBsonSerializer. /// public static class IBsonSerializerExtensions { // methods /// /// Deserializes a value. /// /// The serializer. /// The deserialization context. /// A deserialized value. public static object Deserialize(this IBsonSerializer serializer, BsonDeserializationContext context) { var args = new BsonDeserializationArgs { NominalType = serializer.ValueType }; return serializer.Deserialize(context, args); } /// /// Deserializes a value. /// /// The type that this serializer knows how to serialize. /// The serializer. /// The deserialization context. /// A deserialized value. public static TValue Deserialize(this IBsonSerializer serializer, BsonDeserializationContext context) { var args = new BsonDeserializationArgs { NominalType = serializer.ValueType }; return serializer.Deserialize(context, args); } /// /// Serializes a value. /// /// The serializer. /// The serialization context. /// The value. public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, object value) { var args = new BsonSerializationArgs { NominalType = serializer.ValueType }; serializer.Serialize(context, args, value); } /// /// Serializes a value. /// /// The type that this serializer knows how to serialize. /// The serializer. /// The serialization context. /// The value. public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, TValue value) { var args = new BsonSerializationArgs { NominalType = serializer.ValueType }; serializer.Serialize(context, args, value); } /// /// Converts a value to a BsonValue by serializing it. /// /// The serializer. /// The value. /// The serialized value. public static BsonValue ToBsonValue(this IBsonSerializer serializer, object value) { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { var context = BsonSerializationContext.CreateRoot(writer); writer.WriteStartDocument(); writer.WriteName("x"); serializer.Serialize(context, value); writer.WriteEndDocument(); } return document[0]; } /// /// Converts a value to a BsonValue by serializing it. /// /// The type of the value. /// The serializer. /// The value. /// The serialized value. public static BsonValue ToBsonValue(this IBsonSerializer serializer, TValue value) { var document = new BsonDocument(); using (var writer = new BsonDocumentWriter(document)) { var context = BsonSerializationContext.CreateRoot(writer); writer.WriteStartDocument(); writer.WriteName("x"); serializer.Serialize(context, value); writer.WriteEndDocument(); } return document[0]; } } }