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