ClassSerializerBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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;
  16. namespace MongoDB.Bson.Serialization.Serializers
  17. {
  18. /// <summary>
  19. /// Represents an abstract base class for class serializers.
  20. /// </summary>
  21. /// <typeparam name="TValue">The type of the value.</typeparam>
  22. public abstract class ClassSerializerBase<TValue> : SerializerBase<TValue> where TValue : class
  23. {
  24. // public methods
  25. /// <summary>
  26. /// Deserializes a value.
  27. /// </summary>
  28. /// <param name="context">The deserialization context.</param>
  29. /// <param name="args">The deserialization args.</param>
  30. /// <returns>A deserialized value.</returns>
  31. public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  32. {
  33. var bsonReader = context.Reader;
  34. if (bsonReader.GetCurrentBsonType() == BsonType.Null)
  35. {
  36. bsonReader.ReadNull();
  37. return null;
  38. }
  39. else
  40. {
  41. var actualType = GetActualType(context);
  42. if (actualType == typeof(TValue))
  43. {
  44. return DeserializeValue(context, args);
  45. }
  46. else
  47. {
  48. var serializer = BsonSerializer.LookupSerializer(actualType);
  49. return (TValue)serializer.Deserialize(context, args);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Serializes a value.
  55. /// </summary>
  56. /// <param name="context">The serialization context.</param>
  57. /// <param name="args">The serialization args.</param>
  58. /// <param name="value">The value.</param>
  59. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
  60. {
  61. if (value == null)
  62. {
  63. var bsonWriter = context.Writer;
  64. bsonWriter.WriteNull();
  65. }
  66. else
  67. {
  68. var actualType = value.GetType();
  69. if (actualType == typeof(TValue) || args.SerializeAsNominalType)
  70. {
  71. SerializeValue(context, args, value);
  72. }
  73. else
  74. {
  75. var serializer = BsonSerializer.LookupSerializer(actualType);
  76. serializer.Serialize(context, value);
  77. }
  78. }
  79. }
  80. // protected methods
  81. /// <summary>
  82. /// Deserializes a class.
  83. /// </summary>
  84. /// <param name="context">The deserialization context.</param>
  85. /// <param name="args">The deserialization args.</param>
  86. /// <returns>A deserialized value.</returns>
  87. protected virtual TValue DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  88. {
  89. throw CreateCannotBeDeserializedException();
  90. }
  91. /// <summary>
  92. /// Gets the actual type.
  93. /// </summary>
  94. /// <param name="context">The context.</param>
  95. /// <returns>The actual type.</returns>
  96. protected virtual Type GetActualType(BsonDeserializationContext context)
  97. {
  98. var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(TValue));
  99. return discriminatorConvention.GetActualType(context.Reader, typeof(TValue));
  100. }
  101. /// <summary>
  102. /// Serializes a value of type {TValue}.
  103. /// </summary>
  104. /// <param name="context">The serialization context.</param>
  105. /// <param name="args">The serialization args.</param>
  106. /// <param name="value">The value.</param>
  107. protected virtual void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
  108. {
  109. throw CreateCannotBeSerializedException();
  110. }
  111. }
  112. }