/* Copyright 2018-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 System.Reflection;
using MongoDB.Bson.Serialization.Options;
namespace MongoDB.Bson.Serialization.Serializers
{
///
/// 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 ReadOnlyDictionaryInterfaceImplementerSerializer :
DictionarySerializerBase,
IChildSerializerConfigurable,
IDictionaryRepresentationConfigurable>
where TDictionary : class, IReadOnlyDictionary
{
///
/// Initializes a new instance of the class.
///
public ReadOnlyDictionaryInterfaceImplementerSerializer()
{
}
///
/// Initializes a new instance of the class.
///
/// The dictionary representation.
public ReadOnlyDictionaryInterfaceImplementerSerializer(DictionaryRepresentation dictionaryRepresentation)
: base(dictionaryRepresentation)
{
}
///
/// Initializes a new instance of the class.
///
/// The dictionary representation.
/// The key serializer.
/// The value serializer.
public ReadOnlyDictionaryInterfaceImplementerSerializer(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 ReadOnlyDictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
{
return dictionaryRepresentation == DictionaryRepresentation
? this
: new ReadOnlyDictionaryInterfaceImplementerSerializer(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 ReadOnlyDictionaryInterfaceImplementerSerializer WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation, IBsonSerializer keySerializer, IBsonSerializer valueSerializer)
{
return dictionaryRepresentation == DictionaryRepresentation && keySerializer == KeySerializer && valueSerializer == ValueSerializer
? this
: new ReadOnlyDictionaryInterfaceImplementerSerializer(dictionaryRepresentation, keySerializer, valueSerializer);
}
///
/// Returns a serializer that has been reconfigured with the specified key serializer.
///
/// The key serializer.
/// The reconfigured serializer.
public ReadOnlyDictionaryInterfaceImplementerSerializer WithKeySerializer(IBsonSerializer keySerializer)
{
return keySerializer == KeySerializer
? this
: new ReadOnlyDictionaryInterfaceImplementerSerializer(DictionaryRepresentation, keySerializer, ValueSerializer);
}
///
/// Returns a serializer that has been reconfigured with the specified value serializer.
///
/// The value serializer.
/// The reconfigured serializer.
public ReadOnlyDictionaryInterfaceImplementerSerializer WithValueSerializer(IBsonSerializer valueSerializer)
{
return valueSerializer == ValueSerializer
? this
: new ReadOnlyDictionaryInterfaceImplementerSerializer(DictionaryRepresentation, KeySerializer, valueSerializer);
}
// explicit interface implementations
IBsonSerializer IChildSerializerConfigurable.ChildSerializer => ValueSerializer;
IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
{
return WithValueSerializer((IBsonSerializer)childSerializer);
}
IBsonSerializer IDictionaryRepresentationConfigurable.WithDictionaryRepresentation(DictionaryRepresentation dictionaryRepresentation)
{
return WithDictionaryRepresentation(dictionaryRepresentation);
}
///
protected override ICollection> CreateAccumulator()
{
return new Dictionary();
}
///
protected override TDictionary FinalizeAccumulator(ICollection> accumulator)
{
try
{
return (TDictionary) Activator.CreateInstance(typeof(TDictionary), new object[] {accumulator});
}
catch (MissingMethodException exception)
{
throw new MissingMethodException(
$"No suitable constructor found for IReadOnlyDictionary type: '{typeof(TDictionary).FullName}'.",
exception);
}
}
}
}