SealedClassSerializerBase.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 an abstract base class for sealed class serializers.
  19. /// </summary>
  20. /// <typeparam name="TValue">The type of the value.</typeparam>
  21. public abstract class SealedClassSerializerBase<TValue> : SerializerBase<TValue> where TValue : class
  22. {
  23. // public methods
  24. /// <summary>
  25. /// Deserializes a value.
  26. /// </summary>
  27. /// <param name="context">The deserialization context.</param>
  28. /// <param name="args">The deserialization args.</param>
  29. /// <returns>A deserialized value.</returns>
  30. public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  31. {
  32. var bsonReader = context.Reader;
  33. if (bsonReader.GetCurrentBsonType() == BsonType.Null)
  34. {
  35. bsonReader.ReadNull();
  36. return null;
  37. }
  38. else
  39. {
  40. return DeserializeValue(context, args);
  41. }
  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 value.</param>
  49. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
  50. {
  51. if (value == null)
  52. {
  53. var bsonWriter = context.Writer;
  54. bsonWriter.WriteNull();
  55. }
  56. else
  57. {
  58. SerializeValue(context, args, value);
  59. }
  60. }
  61. // protected methods
  62. /// <summary>
  63. /// Deserializes a class.
  64. /// </summary>
  65. /// <param name="context">The deserialization context.</param>
  66. /// <param name="args">The deserialization args.</param>
  67. /// <returns>A deserialized value.</returns>
  68. protected virtual TValue DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  69. {
  70. throw CreateCannotBeDeserializedException();
  71. }
  72. /// <summary>
  73. /// Serializes a value of type {TValue}.
  74. /// </summary>
  75. /// <param name="context">The serialization context.</param>
  76. /// <param name="args">The serialization args.</param>
  77. /// <param name="value">The value.</param>
  78. protected virtual void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
  79. {
  80. throw CreateCannotBeSerializedException();
  81. }
  82. }
  83. }