RawBsonDocumentSerializer.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 RawBsonDocuments.
  19. /// </summary>
  20. public class RawBsonDocumentSerializer : BsonValueSerializerBase<RawBsonDocument>
  21. {
  22. // private static fields
  23. private static readonly RawBsonDocumentSerializer __instance = new RawBsonDocumentSerializer();
  24. // constructors
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="RawBsonDocumentSerializer"/> class.
  27. /// </summary>
  28. public RawBsonDocumentSerializer()
  29. : base(BsonType.Document)
  30. {
  31. }
  32. // public static properties
  33. /// <summary>
  34. /// Gets the instance.
  35. /// </summary>
  36. /// <value>
  37. /// The instance.
  38. /// </value>
  39. public static RawBsonDocumentSerializer Instance
  40. {
  41. get { return __instance; }
  42. }
  43. // protected methods
  44. /// <summary>
  45. /// Deserializes a value.
  46. /// </summary>
  47. /// <param name="context">The deserialization context.</param>
  48. /// <param name="args">The deserialization args.</param>
  49. /// <returns>A deserialized value.</returns>
  50. protected override RawBsonDocument DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  51. {
  52. var bsonReader = context.Reader;
  53. var slice = bsonReader.ReadRawBsonDocument();
  54. return new RawBsonDocument(slice);
  55. }
  56. /// <summary>
  57. /// Serializes a value.
  58. /// </summary>
  59. /// <param name="context">The serialization context.</param>
  60. /// <param name="args">The serialization args.</param>
  61. /// <param name="value">The object.</param>
  62. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, RawBsonDocument value)
  63. {
  64. var bsonWriter = context.Writer;
  65. bsonWriter.WriteRawBsonDocument(value.Slice);
  66. }
  67. }
  68. }