DiscriminatedInterfaceSerializer.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 System.Reflection;
  17. using MongoDB.Bson.Serialization.Conventions;
  18. namespace MongoDB.Bson.Serialization.Serializers
  19. {
  20. /// <summary>
  21. /// Represents a serializer for Interfaces.
  22. /// </summary>
  23. /// <typeparam name="TInterface">The type of the interface.</typeparam>
  24. public class DiscriminatedInterfaceSerializer<TInterface> : SerializerBase<TInterface> // where TInterface is an interface
  25. {
  26. // private fields
  27. private readonly Type _interfaceType;
  28. private readonly IDiscriminatorConvention _discriminatorConvention;
  29. private readonly IBsonSerializer<object> _objectSerializer;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="DiscriminatedInterfaceSerializer{TInterface}" /> class.
  33. /// </summary>
  34. public DiscriminatedInterfaceSerializer()
  35. : this(BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface)))
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="DiscriminatedInterfaceSerializer{TInterface}" /> class.
  40. /// </summary>
  41. /// <param name="discriminatorConvention">The discriminator convention.</param>
  42. /// <exception cref="System.ArgumentException">interfaceType</exception>
  43. /// <exception cref="System.ArgumentNullException">interfaceType</exception>
  44. public DiscriminatedInterfaceSerializer(IDiscriminatorConvention discriminatorConvention)
  45. {
  46. var interfaceTypeInfo = typeof(TInterface).GetTypeInfo();
  47. if (!interfaceTypeInfo.IsInterface)
  48. {
  49. var message = string.Format("{0} is not an interface.", typeof(TInterface).FullName);
  50. throw new ArgumentException(message, "<TInterface>");
  51. }
  52. _interfaceType = typeof(TInterface);
  53. _discriminatorConvention = discriminatorConvention;
  54. _objectSerializer = new ObjectSerializer(_discriminatorConvention);
  55. }
  56. // public methods
  57. /// <summary>
  58. /// Deserializes a value.
  59. /// </summary>
  60. /// <param name="context">The deserialization context.</param>
  61. /// <param name="args">The deserialization args.</param>
  62. /// <returns>A deserialized value.</returns>
  63. /// <exception cref="System.FormatException"></exception>
  64. public override TInterface Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
  65. {
  66. var bsonReader = context.Reader;
  67. if (bsonReader.GetCurrentBsonType() == BsonType.Null)
  68. {
  69. bsonReader.ReadNull();
  70. return default(TInterface);
  71. }
  72. else
  73. {
  74. var actualType = _discriminatorConvention.GetActualType(bsonReader, typeof(TInterface));
  75. if (actualType == _interfaceType)
  76. {
  77. var message = string.Format("Unable to determine actual type of object to deserialize for interface type {0}.", _interfaceType.FullName);
  78. throw new FormatException(message);
  79. }
  80. var serializer = BsonSerializer.LookupSerializer(actualType);
  81. return (TInterface)serializer.Deserialize(context, args);
  82. }
  83. }
  84. /// <summary>
  85. /// Serializes a value.
  86. /// </summary>
  87. /// <param name="context">The serialization context.</param>
  88. /// <param name="args">The serialization args.</param>
  89. /// <param name="value">The document.</param>
  90. public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TInterface value)
  91. {
  92. var bsonWriter = context.Writer;
  93. if (value == null)
  94. {
  95. bsonWriter.WriteNull();
  96. }
  97. else
  98. {
  99. args.NominalType = typeof(object);
  100. _objectSerializer.Serialize(context, args, value);
  101. }
  102. }
  103. }
  104. }