SerializerBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. using MongoDB.Bson.IO;
  17. using MongoDB.Bson.Serialization.Conventions;
  18. namespace MongoDB.Bson.Serialization.Serializers
  19. {
  20. /// <summary>
  21. /// Represents an abstract base class for serializers.
  22. /// </summary>
  23. /// <typeparam name="TValue">The type of the value.</typeparam>
  24. public abstract class SerializerBase<TValue> : IBsonSerializer<TValue>
  25. {
  26. // public properties
  27. /// <summary>
  28. /// Gets the type of the values.
  29. /// </summary>
  30. /// <value>
  31. /// The type of the values.
  32. /// </value>
  33. public Type ValueType
  34. {
  35. get { return typeof(TValue); }
  36. }
  37. // public methods
  38. /// <summary>
  39. /// Deserializes a value.
  40. /// </summary>
  41. /// <param name="context">The deserialization context.</param>
  42. /// <param name="args">The deserialization args.</param>
  43. /// <returns>A deserialized value.</returns>
  44. public virtual TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  45. {
  46. throw CreateCannotBeDeserializedException();
  47. }
  48. /// <summary>
  49. /// Serializes a value.
  50. /// </summary>
  51. /// <param name="context">The serialization context.</param>
  52. /// <param name="args">The serialization args.</param>
  53. /// <param name="value">The value.</param>
  54. public virtual void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
  55. {
  56. throw CreateCannotBeSerializedException();
  57. }
  58. // protected methods
  59. /// <summary>
  60. /// Creates an exception to throw when a type cannot be deserialized.
  61. /// </summary>
  62. /// <returns>An exception.</returns>
  63. protected Exception CreateCannotBeDeserializedException()
  64. {
  65. var message = string.Format(
  66. "Values of type '{0}' cannot be deserialized using a serializer of type '{1}'.",
  67. BsonUtils.GetFriendlyTypeName(typeof(TValue)),
  68. BsonUtils.GetFriendlyTypeName(GetType()));
  69. return new NotSupportedException(message);
  70. }
  71. /// <summary>
  72. /// Creates an exception to throw when a type cannot be deserialized.
  73. /// </summary>
  74. /// <returns>An exception.</returns>
  75. protected Exception CreateCannotBeSerializedException()
  76. {
  77. var message = string.Format(
  78. "Values of type '{0}' cannot be serialized using a serializer of type '{1}'.",
  79. BsonUtils.GetFriendlyTypeName(typeof(TValue)),
  80. BsonUtils.GetFriendlyTypeName(GetType()));
  81. return new NotSupportedException(message);
  82. }
  83. /// <summary>
  84. /// Creates an exception to throw when a type cannot be deserialized from a BsonType.
  85. /// </summary>
  86. /// <param name="bsonType">The BSON type.</param>
  87. /// <returns>An exception.</returns>
  88. protected Exception CreateCannotDeserializeFromBsonTypeException(BsonType bsonType)
  89. {
  90. var message = string.Format("Cannot deserialize a '{0}' from BsonType '{1}'.",
  91. BsonUtils.GetFriendlyTypeName(ValueType),
  92. bsonType);
  93. return new FormatException(message);
  94. }
  95. /// <summary>
  96. /// Ensures that the BsonType equals the expected type.
  97. /// </summary>
  98. /// <param name="reader">The reader.</param>
  99. /// <param name="bsonType">The expected type.</param>
  100. protected void EnsureBsonTypeEquals(IBsonReader reader, BsonType bsonType)
  101. {
  102. if (reader.GetCurrentBsonType() != bsonType)
  103. {
  104. throw CreateCannotDeserializeFromBsonTypeException(reader.GetCurrentBsonType());
  105. }
  106. }
  107. // explicit interface implementations
  108. object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  109. {
  110. return Deserialize(context, args);
  111. }
  112. void IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
  113. {
  114. Serialize(context, args, (TValue)value);
  115. }
  116. }
  117. }