/* Copyright 2010-2015 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 System.Collections.Generic; using MongoDB.Bson.Serialization.Serializers; namespace MongoDB.Bson.Serialization { /// /// Represents a serializer for TClass (a subclass of BsonDocumentBackedClass). /// /// The subclass of BsonDocumentBackedClass. public abstract class BsonDocumentBackedClassSerializer : ClassSerializerBase, IBsonDocumentSerializer where TClass : BsonDocumentBackedClass { // private fields private readonly Dictionary _memberSerializationInfo; // constructors /// /// Initializes a new instance of the class. /// protected BsonDocumentBackedClassSerializer() { _memberSerializationInfo = new Dictionary(); } // public methods /// /// Deserializes a value. /// /// The deserialization context. /// The deserialization args. /// A deserialized value. public override TClass Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { var backingDocument = BsonDocumentSerializer.Instance.Deserialize(context); return CreateInstance(backingDocument); } /// /// Tries to get the serialization info for a member. /// /// Name of the member. /// The serialization information. /// /// true if the serialization info exists; otherwise false. /// public virtual bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo) { return _memberSerializationInfo.TryGetValue(memberName, out serializationInfo); } /// /// Serializes a value. /// /// The serialization context. /// The serialization args. /// The object. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TClass value) { var backingDocument = ((BsonDocumentBackedClass)value).BackingDocument; BsonDocumentSerializer.Instance.Serialize(context, backingDocument); } // protected methods /// /// Registers a member. /// /// The member name. /// The element name. /// The serializer. protected void RegisterMember(string memberName, string elementName, IBsonSerializer serializer) { if (memberName == null) { throw new ArgumentNullException("memberName"); } if (elementName == null) { throw new ArgumentNullException("elementName"); } if (serializer == null) { throw new ArgumentNullException("serializer"); } var info = new BsonSerializationInfo(elementName, serializer, serializer.ValueType); _memberSerializationInfo.Add(memberName, info); } /// /// Creates the instance. /// /// The backing document. /// An instance of TClass. protected abstract TClass CreateInstance(BsonDocument backingDocument); } }