PrimitiveSerializationProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 2010-2016 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.Collections.Generic;
  17. using System.Globalization;
  18. using System.Net;
  19. using System.Reflection;
  20. using MongoDB.Bson.Serialization.Serializers;
  21. namespace MongoDB.Bson.Serialization
  22. {
  23. /// <summary>
  24. /// Provides serializers for primitive types.
  25. /// </summary>
  26. public class PrimitiveSerializationProvider : BsonSerializationProviderBase
  27. {
  28. private static readonly Dictionary<Type, Type> __serializersTypes;
  29. static PrimitiveSerializationProvider()
  30. {
  31. __serializersTypes = new Dictionary<Type, Type>
  32. {
  33. { typeof(Boolean), typeof(BooleanSerializer) },
  34. { typeof(Byte), typeof(ByteSerializer) },
  35. { typeof(Byte[]), typeof(ByteArraySerializer) },
  36. { typeof(Char), typeof(CharSerializer) },
  37. { typeof(CultureInfo), typeof(CultureInfoSerializer) },
  38. { typeof(DateTime), typeof(DateTimeSerializer) },
  39. { typeof(DateTimeOffset), typeof(DateTimeOffsetSerializer) },
  40. { typeof(Decimal), typeof(DecimalSerializer) },
  41. { typeof(Decimal128), typeof(Decimal128Serializer) },
  42. { typeof(Double), typeof(DoubleSerializer) },
  43. { typeof(Guid), typeof(GuidSerializer) },
  44. { typeof(Int16), typeof(Int16Serializer) },
  45. { typeof(Int32), typeof(Int32Serializer) },
  46. { typeof(Int64), typeof(Int64Serializer) },
  47. { typeof(IPAddress), typeof(IPAddressSerializer) },
  48. { typeof(IPEndPoint), typeof(IPEndPointSerializer) },
  49. { typeof(KeyValuePair<,>), typeof(KeyValuePairSerializer<,>) },
  50. { typeof(Nullable<>), typeof(NullableSerializer<>) },
  51. { typeof(Object), typeof(ObjectSerializer) },
  52. { typeof(ObjectId), typeof(ObjectIdSerializer) },
  53. { typeof(SByte), typeof(SByteSerializer) },
  54. { typeof(Single), typeof(SingleSerializer) },
  55. { typeof(String), typeof(StringSerializer) },
  56. { typeof(TimeSpan), typeof(TimeSpanSerializer) },
  57. { typeof(Tuple<>), typeof(TupleSerializer<>) },
  58. { typeof(Tuple<,>), typeof(TupleSerializer<,>) },
  59. { typeof(Tuple<,,>), typeof(TupleSerializer<,,>) },
  60. { typeof(Tuple<,,,>), typeof(TupleSerializer<,,,>) },
  61. { typeof(Tuple<,,,,>), typeof(TupleSerializer<,,,,>) },
  62. { typeof(Tuple<,,,,,>), typeof(TupleSerializer<,,,,,>) },
  63. { typeof(Tuple<,,,,,,>), typeof(TupleSerializer<,,,,,,>) },
  64. { typeof(Tuple<,,,,,,,>), typeof(TupleSerializer<,,,,,,,>) },
  65. { typeof(UInt16), typeof(UInt16Serializer) },
  66. { typeof(UInt32), typeof(UInt32Serializer) },
  67. { typeof(UInt64), typeof(UInt64Serializer) },
  68. { typeof(Uri), typeof(UriSerializer) },
  69. { typeof(Version), typeof(VersionSerializer) }
  70. };
  71. }
  72. /// <inheritdoc/>
  73. public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
  74. {
  75. if (type == null)
  76. {
  77. throw new ArgumentNullException("type");
  78. }
  79. var typeInfo = type.GetTypeInfo();
  80. if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
  81. {
  82. var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
  83. throw new ArgumentException(message, "type");
  84. }
  85. Type serializerType;
  86. if (__serializersTypes.TryGetValue(type, out serializerType))
  87. {
  88. return CreateSerializer(serializerType, serializerRegistry);
  89. }
  90. if (typeInfo.IsGenericType && !typeInfo.ContainsGenericParameters)
  91. {
  92. Type serializerTypeDefinition;
  93. if (__serializersTypes.TryGetValue(type.GetGenericTypeDefinition(), out serializerTypeDefinition))
  94. {
  95. return CreateGenericSerializer(serializerTypeDefinition, type.GetTypeInfo().GetGenericArguments(), serializerRegistry);
  96. }
  97. }
  98. if (typeInfo.IsEnum)
  99. {
  100. return CreateGenericSerializer(typeof(EnumSerializer<>), new[] { type }, serializerRegistry);
  101. }
  102. return null;
  103. }
  104. }
  105. }