/* Copyright 2010-2015 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.Linq; using MongoDB.Bson.Serialization.Conventions; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { /// /// Represents a serializer for Stacks. /// public class StackSerializer : EnumerableSerializerBase, IChildSerializerConfigurable, IBsonArraySerializer { // constructors /// /// Initializes a new instance of the class. /// public StackSerializer() { } /// /// Initializes a new instance of the class. /// /// The item serializer. public StackSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) { } /// /// Initializes a new instance of the class. /// /// The serializer registry. public StackSerializer(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 StackSerializer WithItemSerializer(IBsonSerializer itemSerializer) { return new StackSerializer(itemSerializer); } // protected methods /// /// Adds the item. /// /// The accumulator. /// The item. protected override void AddItem(object accumulator, object item) { ((Stack)accumulator).Push(item); } /// /// Creates the accumulator. /// /// The accumulator. protected override object CreateAccumulator() { return new Stack(); } /// /// Enumerates the items in serialization order. /// /// The value. /// The items. protected override IEnumerable EnumerateItemsInSerializationOrder(Stack value) { return value.Cast().Reverse(); } /// /// Finalizes the result. /// /// The accumulator. /// The result. protected override Stack FinalizeResult(object accumulator) { return (Stack)accumulator; } // explicit interface implementations IBsonSerializer IChildSerializerConfigurable.ChildSerializer { get { return ItemSerializer; } } IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer) { return WithItemSerializer(childSerializer); } } /// /// Represents a serializer for Stacks. /// /// The type of the elements. public class StackSerializer : EnumerableSerializerBase, TItem>, IChildSerializerConfigurable, IBsonArraySerializer { // constructors /// /// Initializes a new instance of the class. /// public StackSerializer() { } /// /// Initializes a new instance of the class. /// /// The item serializer. public StackSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) { } /// /// Initializes a new instance of the class. /// /// The serializer registry. public StackSerializer(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 StackSerializer WithItemSerializer(IBsonSerializer itemSerializer) { return new StackSerializer(itemSerializer); } // protected methods /// /// Adds the item. /// /// The accumulator. /// The item. protected override void AddItem(object accumulator, TItem item) { ((Stack)accumulator).Push(item); } /// /// Creates the accumulator. /// /// The accumulator. protected override object CreateAccumulator() { return new Stack(); } /// /// Enumerates the items in serialization order. /// /// The value. /// The items. protected override IEnumerable EnumerateItemsInSerializationOrder(Stack value) { return value.Reverse(); } /// /// Finalizes the result. /// /// The accumulator. /// The result. protected override Stack FinalizeResult(object accumulator) { return (Stack)accumulator; } // explicit interface implementations IBsonSerializer IChildSerializerConfigurable.ChildSerializer { get { return ItemSerializer; } } IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer) { return WithItemSerializer((IBsonSerializer)childSerializer); } } }