/* 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 { /// /// Represents all the contextual information needed by a serializer to deserialize a value. /// public class BsonDeserializationContext { // private fields private readonly bool _allowDuplicateElementNames; private readonly IBsonSerializer _dynamicArraySerializer; private readonly IBsonSerializer _dynamicDocumentSerializer; private readonly IBsonReader _reader; // constructors private BsonDeserializationContext( IBsonReader reader, bool allowDuplicateElementNames, IBsonSerializer dynamicArraySerializer, IBsonSerializer dynamicDocumentSerializer) { _reader = reader; _allowDuplicateElementNames = allowDuplicateElementNames; _dynamicArraySerializer = dynamicArraySerializer; _dynamicDocumentSerializer = dynamicDocumentSerializer; } // public properties /// /// Gets a value indicating whether to allow duplicate element names. /// /// /// true if duplicate element names shoud be allowed; otherwise, false. /// public bool AllowDuplicateElementNames { get { return _allowDuplicateElementNames; } } /// /// Gets the dynamic array serializer. /// /// /// The dynamic array serializer. /// public IBsonSerializer DynamicArraySerializer { get { return _dynamicArraySerializer; } } /// /// Gets the dynamic document serializer. /// /// /// The dynamic document serializer. /// public IBsonSerializer DynamicDocumentSerializer { get { return _dynamicDocumentSerializer; } } /// /// Gets the reader. /// /// /// The reader. /// public IBsonReader Reader { get { return _reader; } } // public static methods /// /// Creates a root context. /// /// The reader. /// The configurator. /// /// A root context. /// public static BsonDeserializationContext CreateRoot( IBsonReader reader, Action configurator = null) { var builder = new Builder(null, reader); if (configurator != null) { configurator(builder); } return builder.Build(); } // public methods /// /// Creates a new context with some values changed. /// /// The configurator. /// /// A new context. /// public BsonDeserializationContext With( Action configurator = null) { var builder = new Builder(this, _reader); if (configurator != null) { configurator(builder); } return builder.Build(); } // nested classes /// /// Represents a builder for a BsonDeserializationContext. /// public class Builder { // private fields private bool _allowDuplicateElementNames; private IBsonSerializer _dynamicArraySerializer; private IBsonSerializer _dynamicDocumentSerializer; private IBsonReader _reader; // constructors internal Builder(BsonDeserializationContext other, IBsonReader reader) { if (reader == null) { throw new ArgumentNullException("reader"); } _reader = reader; if (other != null) { _allowDuplicateElementNames = other.AllowDuplicateElementNames; _dynamicArraySerializer = other.DynamicArraySerializer; _dynamicDocumentSerializer = other.DynamicDocumentSerializer; } else { _dynamicArraySerializer = BsonDefaults.DynamicArraySerializer; _dynamicDocumentSerializer = BsonDefaults.DynamicDocumentSerializer; } } // properties /// /// Gets or sets a value indicating whether to allow duplicate element names. /// /// /// true if duplicate element names should be allowed; otherwise, false. /// public bool AllowDuplicateElementNames { get { return _allowDuplicateElementNames; } set { _allowDuplicateElementNames = value; } } /// /// Gets or sets the dynamic array serializer. /// /// /// The dynamic array serializer. /// public IBsonSerializer DynamicArraySerializer { get { return _dynamicArraySerializer; } set { _dynamicArraySerializer = value; } } /// /// Gets or sets the dynamic document serializer. /// /// /// The dynamic document serializer. /// public IBsonSerializer DynamicDocumentSerializer { get { return _dynamicDocumentSerializer; } set { _dynamicDocumentSerializer = value; } } /// /// Gets the reader. /// /// /// The reader. /// public IBsonReader Reader { get { return _reader; } } // public methods /// /// Builds the BsonDeserializationContext instance. /// /// A BsonDeserializationContext. internal BsonDeserializationContext Build() { return new BsonDeserializationContext(_reader, _allowDuplicateElementNames, _dynamicArraySerializer, _dynamicDocumentSerializer); } } } }