BsonObjectModelSerializationProvider.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.Collections.Generic;
  17. using System.Reflection;
  18. using MongoDB.Bson.Serialization.Serializers;
  19. namespace MongoDB.Bson.Serialization
  20. {
  21. /// <summary>
  22. /// Provides serializers for BsonValue and its derivations.
  23. /// </summary>
  24. public class BsonObjectModelSerializationProvider : BsonSerializationProviderBase
  25. {
  26. private static readonly Dictionary<Type, IBsonSerializer> __serializers;
  27. static BsonObjectModelSerializationProvider()
  28. {
  29. __serializers = new Dictionary<Type, IBsonSerializer>
  30. {
  31. { typeof(BsonArray), BsonArraySerializer.Instance },
  32. { typeof(BsonBinaryData), BsonBinaryDataSerializer.Instance },
  33. { typeof(BsonBoolean), BsonBooleanSerializer.Instance },
  34. { typeof(BsonDateTime), BsonDateTimeSerializer.Instance },
  35. { typeof(BsonDecimal128), BsonDecimal128Serializer.Instance },
  36. { typeof(BsonDocument), BsonDocumentSerializer.Instance },
  37. { typeof(BsonDocumentWrapper), BsonDocumentWrapperSerializer.Instance },
  38. { typeof(BsonDouble), BsonDoubleSerializer.Instance },
  39. { typeof(BsonInt32), BsonInt32Serializer.Instance },
  40. { typeof(BsonInt64), BsonInt64Serializer.Instance },
  41. { typeof(BsonJavaScript), BsonJavaScriptSerializer.Instance },
  42. { typeof(BsonJavaScriptWithScope), BsonJavaScriptWithScopeSerializer.Instance },
  43. { typeof(BsonMaxKey), BsonMaxKeySerializer.Instance },
  44. { typeof(BsonMinKey), BsonMinKeySerializer.Instance },
  45. { typeof(BsonNull), BsonNullSerializer.Instance },
  46. { typeof(BsonObjectId), BsonObjectIdSerializer.Instance },
  47. { typeof(BsonRegularExpression), BsonRegularExpressionSerializer.Instance },
  48. { typeof(BsonString), BsonStringSerializer.Instance },
  49. { typeof(BsonSymbol), BsonSymbolSerializer.Instance },
  50. { typeof(BsonTimestamp), BsonTimestampSerializer.Instance },
  51. { typeof(BsonUndefined), BsonUndefinedSerializer.Instance },
  52. { typeof(BsonValue), BsonValueSerializer.Instance }
  53. };
  54. }
  55. /// <inheritdoc/>
  56. public override IBsonSerializer GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
  57. {
  58. if (type == null)
  59. {
  60. throw new ArgumentNullException("type");
  61. }
  62. var typeInfo = type.GetTypeInfo();
  63. if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
  64. {
  65. var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
  66. throw new ArgumentException(message, "type");
  67. }
  68. IBsonSerializer serializer;
  69. if (__serializers.TryGetValue(type, out serializer))
  70. {
  71. return serializer;
  72. }
  73. return null;
  74. }
  75. }
  76. }