/* 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.Collections.Generic; namespace MongoDB.Bson.Serialization.Serializers { /// /// Represents a serializer for one-dimensional arrays. /// /// The type of the elements. public class ArraySerializer : EnumerableSerializerBase, IBsonArraySerializer, IChildSerializerConfigurable { // constructors /// /// Initializes a new instance of the class. /// public ArraySerializer() { } /// /// Initializes a new instance of the class. /// /// The item serializer. public ArraySerializer(IBsonSerializer itemSerializer) : base(itemSerializer) { } /// /// Initializes a new instance of the class. /// /// The serializer registry. public ArraySerializer(IBsonSerializerRegistry serializerRegistry) : base(serializerRegistry) { } // public methods /// /// Returns a serializer that has been reconfigured with the specified item serializer. /// /// The item serializer. /// The reconfigured serializer. public ArraySerializer WithItemSerializer(IBsonSerializer itemSerializer) { return new ArraySerializer(itemSerializer); } // protected methods /// /// Adds the item. /// /// The accumulator. /// The item. protected override void AddItem(object accumulator, TItem item) { ((List)accumulator).Add(item); } /// /// Creates the accumulator. /// /// The accumulator. protected override object CreateAccumulator() { return new List(); } /// /// Enumerates the items in serialization order. /// /// The value. /// The items. protected override IEnumerable EnumerateItemsInSerializationOrder(TItem[] value) { return (IEnumerable)value; } /// /// Finalizes the result. /// /// The accumulator. /// The result. protected override TItem[] FinalizeResult(object accumulator) { return ((List)accumulator).ToArray(); } // explicit interface implementations IBsonSerializer IChildSerializerConfigurable.ChildSerializer { get { return ItemSerializer; } } IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer) { return WithItemSerializer((IBsonSerializer)childSerializer); } } }