BsonValueSerializerBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 base class for BsonValue serializers.
  19. /// </summary>
  20. /// <typeparam name="TBsonValue">The type of the BsonValue.</typeparam>
  21. public abstract class BsonValueSerializerBase<TBsonValue> : SerializerBase<TBsonValue> where TBsonValue : BsonValue
  22. {
  23. // private fields
  24. private readonly BsonType? _bsonType;
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="BsonValueSerializerBase{TBsonValue}"/> class.
  28. /// </summary>
  29. /// <param name="bsonType">The Bson type.</param>
  30. protected BsonValueSerializerBase(BsonType? bsonType)
  31. {
  32. _bsonType = bsonType;
  33. }
  34. // public methods
  35. /// <summary>
  36. /// Deserializes a value.
  37. /// </summary>
  38. /// <param name="context">The deserialization context.</param>
  39. /// <param name="args">The deserialization args.</param>
  40. /// <returns>A deserialized value.</returns>
  41. public override TBsonValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  42. {
  43. if (_bsonType.HasValue)
  44. {
  45. EnsureBsonTypeEquals(context.Reader, _bsonType.Value);
  46. }
  47. return DeserializeValue(context, args);
  48. }
  49. /// <summary>
  50. /// Serializes a value.
  51. /// </summary>
  52. /// <param name="context">The serialization context.</param>
  53. /// <param name="args">The serialization args.</param>
  54. /// <param name="value">The object.</param>
  55. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value)
  56. {
  57. if (value == null)
  58. {
  59. var message = string.Format(
  60. "C# null values of type '{0}' cannot be serialized using a serializer of type '{1}'.",
  61. BsonUtils.GetFriendlyTypeName(ValueType),
  62. BsonUtils.GetFriendlyTypeName(GetType()));
  63. throw new BsonSerializationException(message);
  64. }
  65. var actualType = value.GetType();
  66. if (actualType != ValueType && !args.SerializeAsNominalType)
  67. {
  68. var serializer = BsonSerializer.LookupSerializer(actualType);
  69. serializer.Serialize(context, value);
  70. return;
  71. }
  72. SerializeValue(context, args, value);
  73. }
  74. // protected methods
  75. /// <summary>
  76. /// Deserializes a value.
  77. /// </summary>
  78. /// <param name="context">The deserialization context.</param>
  79. /// <param name="args">The deserialization args.</param>
  80. /// <returns>A deserialized value.</returns>
  81. protected abstract TBsonValue DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args);
  82. /// <summary>
  83. /// Serializes a value.
  84. /// </summary>
  85. /// <param name="context">The serialization context.</param>
  86. /// <param name="args">The serialization args.</param>
  87. /// <param name="value">The object.</param>
  88. protected abstract void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value);
  89. }
  90. }