BsonArraySerializer.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. namespace MongoDB.Bson.Serialization.Serializers
  16. {
  17. /// <summary>
  18. /// Represents a serializer for BsonArrays.
  19. /// </summary>
  20. public class BsonArraySerializer : BsonValueSerializerBase<BsonArray>, IBsonArraySerializer
  21. {
  22. // private static fields
  23. private static BsonArraySerializer __instance = new BsonArraySerializer();
  24. // constructors
  25. /// <summary>
  26. /// Initializes a new instance of the BsonArraySerializer class.
  27. /// </summary>
  28. public BsonArraySerializer()
  29. : base(BsonType.Array)
  30. {
  31. }
  32. // public static properties
  33. /// <summary>
  34. /// Gets an instance of the BsonArraySerializer class.
  35. /// </summary>
  36. public static BsonArraySerializer Instance
  37. {
  38. get { return __instance; }
  39. }
  40. // public methods
  41. /// <summary>
  42. /// Deserializes a value.
  43. /// </summary>
  44. /// <param name="context">The deserialization context.</param>
  45. /// <param name="args">The deserialization args.</param>
  46. /// <returns>A deserialized value.</returns>
  47. protected override BsonArray DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  48. {
  49. var bsonReader = context.Reader;
  50. bsonReader.ReadStartArray();
  51. var array = new BsonArray();
  52. while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
  53. {
  54. var item = BsonValueSerializer.Instance.Deserialize(context);
  55. array.Add(item);
  56. }
  57. bsonReader.ReadEndArray();
  58. return array;
  59. }
  60. /// <summary>
  61. /// Tries to get the serialization info for the individual items of the array.
  62. /// </summary>
  63. /// <param name="serializationInfo">The serialization information.</param>
  64. /// <returns>
  65. /// <c>true</c> if the serialization info exists; otherwise <c>false</c>.
  66. /// </returns>
  67. public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo)
  68. {
  69. serializationInfo = new BsonSerializationInfo(
  70. null,
  71. BsonValueSerializer.Instance,
  72. typeof(BsonValue));
  73. return true;
  74. }
  75. // protected methods
  76. /// <summary>
  77. /// Serializes a value.
  78. /// </summary>
  79. /// <param name="context">The serialization context.</param>
  80. /// <param name="args">The serialization args.</param>
  81. /// <param name="value">The object.</param>
  82. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, BsonArray value)
  83. {
  84. var bsonWriter = context.Writer;
  85. bsonWriter.WriteStartArray();
  86. for (int i = 0; i < value.Count; i++)
  87. {
  88. BsonValueSerializer.Instance.Serialize(context, value[i]);
  89. }
  90. bsonWriter.WriteEndArray();
  91. }
  92. }
  93. }