ArraySerializer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System.Collections.Generic;
  16. namespace MongoDB.Bson.Serialization.Serializers
  17. {
  18. /// <summary>
  19. /// Represents a serializer for one-dimensional arrays.
  20. /// </summary>
  21. /// <typeparam name="TItem">The type of the elements.</typeparam>
  22. public class ArraySerializer<TItem> :
  23. EnumerableSerializerBase<TItem[], TItem>,
  24. IBsonArraySerializer,
  25. IChildSerializerConfigurable
  26. {
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ArraySerializer{TItem}"/> class.
  30. /// </summary>
  31. public ArraySerializer()
  32. {
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="ArraySerializer{TItem}"/> class.
  36. /// </summary>
  37. /// <param name="itemSerializer">The item serializer.</param>
  38. public ArraySerializer(IBsonSerializer<TItem> itemSerializer)
  39. : base(itemSerializer)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="ArraySerializer{TItem}" /> class.
  44. /// </summary>
  45. /// <param name="serializerRegistry">The serializer registry.</param>
  46. public ArraySerializer(IBsonSerializerRegistry serializerRegistry)
  47. : base(serializerRegistry)
  48. {
  49. }
  50. // public methods
  51. /// <summary>
  52. /// Returns a serializer that has been reconfigured with the specified item serializer.
  53. /// </summary>
  54. /// <param name="itemSerializer">The item serializer.</param>
  55. /// <returns>The reconfigured serializer.</returns>
  56. public ArraySerializer<TItem> WithItemSerializer(IBsonSerializer<TItem> itemSerializer)
  57. {
  58. return new ArraySerializer<TItem>(itemSerializer);
  59. }
  60. // protected methods
  61. /// <summary>
  62. /// Adds the item.
  63. /// </summary>
  64. /// <param name="accumulator">The accumulator.</param>
  65. /// <param name="item">The item.</param>
  66. protected override void AddItem(object accumulator, TItem item)
  67. {
  68. ((List<TItem>)accumulator).Add(item);
  69. }
  70. /// <summary>
  71. /// Creates the accumulator.
  72. /// </summary>
  73. /// <returns>The accumulator.</returns>
  74. protected override object CreateAccumulator()
  75. {
  76. return new List<TItem>();
  77. }
  78. /// <summary>
  79. /// Enumerates the items in serialization order.
  80. /// </summary>
  81. /// <param name="value">The value.</param>
  82. /// <returns>The items.</returns>
  83. protected override IEnumerable<TItem> EnumerateItemsInSerializationOrder(TItem[] value)
  84. {
  85. return (IEnumerable<TItem>)value;
  86. }
  87. /// <summary>
  88. /// Finalizes the result.
  89. /// </summary>
  90. /// <param name="accumulator">The accumulator.</param>
  91. /// <returns>The result.</returns>
  92. protected override TItem[] FinalizeResult(object accumulator)
  93. {
  94. return ((List<TItem>)accumulator).ToArray();
  95. }
  96. // explicit interface implementations
  97. IBsonSerializer IChildSerializerConfigurable.ChildSerializer
  98. {
  99. get { return ItemSerializer; }
  100. }
  101. IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
  102. {
  103. return WithItemSerializer((IBsonSerializer<TItem>)childSerializer);
  104. }
  105. }
  106. }