LazyBsonArraySerializer.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 LazyBsonArrays.
  19. /// </summary>
  20. public class LazyBsonArraySerializer : BsonValueSerializerBase<LazyBsonArray>
  21. {
  22. // constructors
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="LazyBsonArraySerializer"/> class.
  25. /// </summary>
  26. public LazyBsonArraySerializer()
  27. : base(BsonType.Array)
  28. {
  29. }
  30. // protected methods
  31. /// <summary>
  32. /// Deserializes a value.
  33. /// </summary>
  34. /// <param name="context">The deserialization context.</param>
  35. /// <param name="args">The deserialization args.</param>
  36. /// <returns>A deserialized value.</returns>
  37. protected override LazyBsonArray DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  38. {
  39. var bsonReader = context.Reader;
  40. var slice = bsonReader.ReadRawBsonArray();
  41. return new LazyBsonArray(slice);
  42. }
  43. /// <summary>
  44. /// Serializes a value.
  45. /// </summary>
  46. /// <param name="context">The serialization context.</param>
  47. /// <param name="args">The serialization args.</param>
  48. /// <param name="value">The object.</param>
  49. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, LazyBsonArray value)
  50. {
  51. var bsonWriter = context.Writer;
  52. var slice = value.Slice;
  53. if (slice == null)
  54. {
  55. BsonArraySerializer.Instance.Serialize(context, value);
  56. }
  57. else
  58. {
  59. bsonWriter.WriteRawBsonArray(slice);
  60. }
  61. }
  62. }
  63. }