/* 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 System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { /// /// Represents a serializer for a class that implements IDictionary. /// /// The type of the dictionary. public class DictionaryInterfaceImplementerSerializer : DictionarySerializerBase, IChildSerializerConfigurable, IDictionaryRepresentationConfigurable where TDictionary : class, IDictionary, new() { /// /// Initializes a new instance of the class. /// public DictionaryInterfaceImplementerSerializer() { } /// /// Initializes a new instance of the class. /// /// The dictionary representation. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation) : base(dictionaryRepresentation) { } /// /// Initializes a new instance of the class. /// /// The dictionary representation. /// The key serializer. /// The value serializer. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) : base(dictionaryRepresentation, keySerializer, valueSerializer) { } // public methods /// /// Returns a serializer that has been reconfigured with the specified dictionary representation. /// /// The dictionary representation. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { if (dictionaryRepresentation == DictionaryRepresentation) { return this; } else { return new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation, KeySerializer, ValueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers. /// /// The dictionary representation. /// The key serializer. /// The value serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { if (dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation, keySerializer, valueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified key serializer. /// /// The key serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithKeySerializer(IBsonSerializer keySerializer) { if (keySerializer == KeySerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(DictionaryRepresentation, keySerializer, ValueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified value serializer. /// /// The value serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithValueSerializer(IBsonSerializer valueSerializer) { if (valueSerializer == ValueSerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(DictionaryRepresentation, KeySerializer, valueSerializer); } } // protected methods /// /// Creates the instance. /// /// The instance. protected override TDictionary CreateInstance() { return new TDictionary(); } // explicit interface implementations IBsonSerializer IChildSerializerConfigurable.ChildSerializer { get { return ValueSerializer; } } IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer) { return WithValueSerializer(childSerializer); } IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { return WithDictionaryRepresentation(dictionaryRepresentation); } } /// /// Represents a serializer for a class that implements . /// /// The type of the dictionary. /// The type of the key. /// The type of the value. public class DictionaryInterfaceImplementerSerializer : DictionarySerializerBase, IChildSerializerConfigurable, IDictionaryRepresentationConfigurable> where TDictionary : class, IDictionary { /// /// Initializes a new instance of the class. /// public DictionaryInterfaceImplementerSerializer() { } /// /// Initializes a new instance of the class. /// /// The dictionary representation. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation) : base(dictionaryRepresentation) { } /// /// Initializes a new instance of the class. /// /// The dictionary representation. /// The key serializer. /// The value serializer. public DictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) : base(dictionaryRepresentation, keySerializer, valueSerializer) { } // public methods /// /// Returns a serializer that has been reconfigured with the specified dictionary representation. /// /// The dictionary representation. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { if (dictionaryRepresentation == DictionaryRepresentation) { return this; } else { return new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation, KeySerializer, ValueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified dictionary representation and key value serializers. /// /// The dictionary representation. /// The key serializer. /// The value serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer) { if (dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation, keySerializer, valueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified key serializer. /// /// The key serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithKeySerializer(IBsonSerializer keySerializer) { if (keySerializer == KeySerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(DictionaryRepresentation, keySerializer, ValueSerializer); } } /// /// Returns a serializer that has been reconfigured with the specified value serializer. /// /// The value serializer. /// The reconfigured serializer. public DictionaryInterfaceImplementerSerializer WithValueSerializer(IBsonSerializer valueSerializer) { if (valueSerializer == ValueSerializer) { return this; } else { return new DictionaryInterfaceImplementerSerializer(DictionaryRepresentation, KeySerializer, valueSerializer); } } // explicit interface implementations IBsonSerializer IChildSerializerConfigurable.ChildSerializer { get { return ValueSerializer; } } IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer) { return WithValueSerializer((IBsonSerializer)childSerializer); } IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation) { return WithDictionaryRepresentation(dictionaryRepresentation); } /// protected override ICollection> CreateAccumulator() { return Activator.CreateInstance(); } } }