EnumRepresentationConvention.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Options;
  18. namespace MongoDB.Bson.Serialization.Conventions
  19. {
  20. /// <summary>
  21. /// A convention that allows you to set the Enum serialization representation
  22. /// </summary>
  23. public class EnumRepresentationConvention : ConventionBase, IMemberMapConvention
  24. {
  25. // private fields
  26. private readonly BsonType _representation;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="EnumRepresentationConvention" /> class.
  30. /// </summary>
  31. /// <param name="representation">The serialization representation. 0 is used to detect representation
  32. /// from the enum itself.</param>
  33. public EnumRepresentationConvention(BsonType representation)
  34. {
  35. EnsureRepresentationIsValidForEnums(representation);
  36. _representation = representation;
  37. }
  38. /// <summary>
  39. /// Gets the representation.
  40. /// </summary>
  41. public BsonType Representation => _representation;
  42. /// <summary>
  43. /// Applies a modification to the member map.
  44. /// </summary>
  45. /// <param name="memberMap">The member map.</param>
  46. public void Apply(BsonMemberMap memberMap)
  47. {
  48. var memberType = memberMap.MemberType;
  49. var memberTypeInfo = memberType.GetTypeInfo();
  50. if (memberTypeInfo.IsEnum)
  51. {
  52. var serializer = memberMap.GetSerializer();
  53. var representationConfigurableSerializer = serializer as IRepresentationConfigurable;
  54. if (representationConfigurableSerializer != null)
  55. {
  56. var reconfiguredSerializer = representationConfigurableSerializer.WithRepresentation(_representation);
  57. memberMap.SetSerializer(reconfiguredSerializer);
  58. }
  59. return;
  60. }
  61. if (IsNullableEnum(memberType))
  62. {
  63. var serializer = memberMap.GetSerializer();
  64. var childSerializerConfigurableSerializer = serializer as IChildSerializerConfigurable;
  65. if (childSerializerConfigurableSerializer != null)
  66. {
  67. var childSerializer = childSerializerConfigurableSerializer.ChildSerializer;
  68. var representationConfigurableChildSerializer = childSerializer as IRepresentationConfigurable;
  69. if (representationConfigurableChildSerializer != null)
  70. {
  71. var reconfiguredChildSerializer = representationConfigurableChildSerializer.WithRepresentation(_representation);
  72. var reconfiguredSerializer = childSerializerConfigurableSerializer.WithChildSerializer(reconfiguredChildSerializer);
  73. memberMap.SetSerializer(reconfiguredSerializer);
  74. }
  75. }
  76. return;
  77. }
  78. }
  79. // private methods
  80. private bool IsNullableEnum(Type type)
  81. {
  82. return
  83. type.GetTypeInfo().IsGenericType &&
  84. type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
  85. Nullable.GetUnderlyingType(type).GetTypeInfo().IsEnum;
  86. }
  87. private void EnsureRepresentationIsValidForEnums(BsonType representation)
  88. {
  89. if (
  90. representation == 0 ||
  91. representation == BsonType.String ||
  92. representation == BsonType.Int32 ||
  93. representation == BsonType.Int64)
  94. {
  95. return;
  96. }
  97. throw new ArgumentException("Enums can only be represented as String, Int32, Int64 or the type of the enum", "representation");
  98. }
  99. }
  100. }