| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- /* 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 MongoDB.Bson.Serialization.Conventions;
- namespace MongoDB.Bson.Serialization.Serializers
- {
- /// <summary>
- /// Represents a base serializer for enumerable values.
- /// </summary>
- /// <typeparam name="TValue">The type of the value.</typeparam>
- public abstract class EnumerableSerializerBase<TValue> : SerializerBase<TValue>, IBsonArraySerializer where TValue : class, IEnumerable
- {
- // private fields
- private readonly IDiscriminatorConvention _discriminatorConvention = new ScalarDiscriminatorConvention("_t");
- private readonly Lazy<IBsonSerializer> _lazyItemSerializer;
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue}"/> class.
- /// </summary>
- protected EnumerableSerializerBase()
- : this(BsonSerializer.SerializerRegistry)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue}"/> class.
- /// </summary>
- /// <param name="itemSerializer">The item serializer.</param>
- protected EnumerableSerializerBase(IBsonSerializer itemSerializer)
- {
- if (itemSerializer == null)
- {
- throw new ArgumentNullException("itemSerializer");
- }
- _lazyItemSerializer = new Lazy<IBsonSerializer>(() => itemSerializer);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue}" /> class.
- /// </summary>
- /// <param name="serializerRegistry">The serializer registry.</param>
- protected EnumerableSerializerBase(IBsonSerializerRegistry serializerRegistry)
- {
- if (serializerRegistry == null)
- {
- throw new ArgumentNullException("serializerRegistry");
- }
- _lazyItemSerializer = new Lazy<IBsonSerializer>(() => serializerRegistry.GetSerializer(typeof(object)));
- }
- /// <summary>
- /// Gets the item serializer.
- /// </summary>
- /// <value>
- /// The item serializer.
- /// </value>
- public IBsonSerializer ItemSerializer
- {
- get { return _lazyItemSerializer.Value; }
- }
- // public methods
- /// <summary>
- /// Deserializes a value.
- /// </summary>
- /// <param name="context">The deserialization context.</param>
- /// <param name="args">The deserialization args.</param>
- /// <returns>A deserialized value.</returns>
- public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
- {
- var bsonReader = context.Reader;
- var bsonType = bsonReader.GetCurrentBsonType();
- switch (bsonType)
- {
- case BsonType.Null:
- bsonReader.ReadNull();
- return null;
- case BsonType.Array:
- bsonReader.ReadStartArray();
- var accumulator = CreateAccumulator();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- var item = _lazyItemSerializer.Value.Deserialize(context);
- AddItem(accumulator, item);
- }
- bsonReader.ReadEndArray();
- return FinalizeResult(accumulator);
- case BsonType.Document:
- var serializer = new DiscriminatedWrapperSerializer<TValue>(_discriminatorConvention, this);
- if (serializer.IsPositionedAtDiscriminatedWrapper(context))
- {
- return (TValue)serializer.Deserialize(context);
- }
- else
- {
- goto default;
- }
- default:
- throw CreateCannotDeserializeFromBsonTypeException(bsonType);
- }
- }
- /// <summary>
- /// Tries to get the serialization info for the individual items of the array.
- /// </summary>
- /// <param name="serializationInfo">The serialization information.</param>
- /// <returns>
- /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
- /// </returns>
- public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
- {
- var itemSerializer = _lazyItemSerializer.Value;
- serializationInfo = new BsonSerializationInfo(null, itemSerializer, itemSerializer.ValueType);
- return true;
- }
- /// <summary>
- /// Serializes a value.
- /// </summary>
- /// <param name="context">The serialization context.</param>
- /// <param name="args">The serialization args.</param>
- /// <param name="value">The object.</param>
- public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
- {
- var bsonWriter = context.Writer;
- if (value == null)
- {
- bsonWriter.WriteNull();
- }
- else
- {
- var actualType = value.GetType();
- if (actualType == args.NominalType || args.SerializeAsNominalType)
- {
- bsonWriter.WriteStartArray();
- foreach (var item in EnumerateItemsInSerializationOrder(value))
- {
- _lazyItemSerializer.Value.Serialize(context, item);
- }
- bsonWriter.WriteEndArray();
- }
- else
- {
- var serializer = new DiscriminatedWrapperSerializer<TValue>(_discriminatorConvention, this);
- serializer.Serialize(context, value);
- }
- }
- }
- // protected methods
- /// <summary>
- /// Adds the item.
- /// </summary>
- /// <param name="accumulator">The accumulator.</param>
- /// <param name="item">The item.</param>
- protected abstract void AddItem(object accumulator, object item);
- /// <summary>
- /// Creates the accumulator.
- /// </summary>
- /// <returns>The accumulator.</returns>
- protected abstract object CreateAccumulator();
- /// <summary>
- /// Enumerates the items in serialization order.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns>The items.</returns>
- protected abstract IEnumerable EnumerateItemsInSerializationOrder(TValue value);
- /// <summary>
- /// Finalizes the result.
- /// </summary>
- /// <param name="accumulator">The accumulator.</param>
- /// <returns>The final result.</returns>
- protected abstract TValue FinalizeResult(object accumulator);
- }
- /// <summary>
- /// Represents a serializer for enumerable values.
- /// </summary>
- /// <typeparam name="TValue">The type of the value.</typeparam>
- /// <typeparam name="TItem">The type of the items.</typeparam>
- public abstract class EnumerableSerializerBase<TValue, TItem> : SerializerBase<TValue>, IBsonArraySerializer where TValue : class, IEnumerable<TItem>
- {
- // private fields
- private readonly IDiscriminatorConvention _discriminatorConvention = new ScalarDiscriminatorConvention("_t");
- private readonly Lazy<IBsonSerializer<TItem>> _lazyItemSerializer;
- // constructors
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue, TItem}"/> class.
- /// </summary>
- protected EnumerableSerializerBase()
- : this(BsonSerializer.SerializerRegistry)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue, TItem}"/> class.
- /// </summary>
- /// <param name="itemSerializer">The item serializer.</param>
- protected EnumerableSerializerBase(IBsonSerializer<TItem> itemSerializer)
- {
- if (itemSerializer == null)
- {
- throw new ArgumentNullException("itemSerializer");
- }
- _lazyItemSerializer = new Lazy<IBsonSerializer<TItem>>(() => itemSerializer);
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="EnumerableSerializerBase{TValue, TItem}" /> class.
- /// </summary>
- /// <param name="serializerRegistry">The serializer registry.</param>
- protected EnumerableSerializerBase(IBsonSerializerRegistry serializerRegistry)
- {
- if (serializerRegistry == null)
- {
- throw new ArgumentNullException("serializerRegistry");
- }
- _lazyItemSerializer = new Lazy<IBsonSerializer<TItem>>(() => serializerRegistry.GetSerializer<TItem>());
- }
- // public properties
- /// <summary>
- /// Gets the item serializer.
- /// </summary>
- /// <value>
- /// The item serializer.
- /// </value>
- public IBsonSerializer<TItem> ItemSerializer
- {
- get { return _lazyItemSerializer.Value; }
- }
- // public methods
- /// <summary>
- /// Deserializes a value.
- /// </summary>
- /// <param name="context">The deserialization context.</param>
- /// <param name="args">The deserialization args.</param>
- /// <returns>A deserialized value.</returns>
- public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
- {
- var bsonReader = context.Reader;
- var bsonType = bsonReader.GetCurrentBsonType();
- switch (bsonType)
- {
- case BsonType.Null:
- bsonReader.ReadNull();
- return null;
- case BsonType.Array:
- bsonReader.ReadStartArray();
- var accumulator = CreateAccumulator();
- while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
- {
- var item = _lazyItemSerializer.Value.Deserialize(context);
- AddItem(accumulator, item);
- }
- bsonReader.ReadEndArray();
- return FinalizeResult(accumulator);
- case BsonType.Document:
- var serializer = new DiscriminatedWrapperSerializer<TValue>(_discriminatorConvention, this);
- if (serializer.IsPositionedAtDiscriminatedWrapper(context))
- {
- return (TValue)serializer.Deserialize(context);
- }
- else
- {
- goto default;
- }
- default:
- throw CreateCannotDeserializeFromBsonTypeException(bsonType);
- }
- }
- /// <summary>
- /// Tries to get the serialization info for the individual items of the array.
- /// </summary>
- /// <param name="serializationInfo">The serialization information.</param>
- /// <returns>
- /// The serialization info for the items.
- /// </returns>
- public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
- {
- var serializer = _lazyItemSerializer.Value;
- serializationInfo = new BsonSerializationInfo(null, serializer, serializer.ValueType);
- return true;
- }
- /// <summary>
- /// Serializes a value.
- /// </summary>
- /// <param name="context">The serialization context.</param>
- /// <param name="args">The serialization args.</param>
- /// <param name="value">The object.</param>
- public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
- {
- var bsonWriter = context.Writer;
- if (value == null)
- {
- bsonWriter.WriteNull();
- }
- else
- {
- var actualType = value.GetType();
- if (actualType == args.NominalType)
- {
- bsonWriter.WriteStartArray();
- foreach (var item in EnumerateItemsInSerializationOrder(value))
- {
- _lazyItemSerializer.Value.Serialize(context, item);
- }
- bsonWriter.WriteEndArray();
- }
- else
- {
- var serializer = new DiscriminatedWrapperSerializer<TValue>(_discriminatorConvention, this);
- serializer.Serialize(context, value);
- }
- }
- }
- // protected methods
- /// <summary>
- /// Adds the item.
- /// </summary>
- /// <param name="accumulator">The accumulator.</param>
- /// <param name="item">The item.</param>
- protected abstract void AddItem(object accumulator, TItem item);
- /// <summary>
- /// Creates the accumulator.
- /// </summary>
- /// <returns>The accumulator.</returns>
- protected abstract object CreateAccumulator();
- /// <summary>
- /// Enumerates the items in serialization order.
- /// </summary>
- /// <param name="value">The value.</param>
- /// <returns>The items.</returns>
- protected abstract IEnumerable<TItem> EnumerateItemsInSerializationOrder(TValue value);
- /// <summary>
- /// Finalizes the result.
- /// </summary>
- /// <param name="accumulator">The accumulator.</param>
- /// <returns>The result.</returns>
- protected abstract TValue FinalizeResult(object accumulator);
- }
- }
|