You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB

  1. /* Copyright 2010 10gen 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.IO;
  18. using System.Linq;
  19. using System.Reflection;
  20. using System.Text;
  21. using System.Text.RegularExpressions;
  22. using MongoDB.Bson.IO;
  23. using MongoDB.Bson.Serialization;
  24. namespace MongoDB.Bson.DefaultSerializer {
  25. public class BsonDefaultSerializationProvider : IBsonSerializationProvider {
  26. #region private static fields
  27. private static BsonDefaultSerializationProvider singleton = new BsonDefaultSerializationProvider();
  28. #endregion
  29. #region constructors
  30. private BsonDefaultSerializationProvider() {
  31. }
  32. #endregion
  33. #region public static properties
  34. public static BsonDefaultSerializationProvider Singleton {
  35. get { return singleton; }
  36. }
  37. #endregion
  38. #region public static methods
  39. public static void Initialize() {
  40. RegisterSerializers();
  41. }
  42. #endregion
  43. #region private static methods
  44. // automatically register all BsonSerializers found in the Bson library
  45. private static void RegisterSerializers() {
  46. var assembly = Assembly.GetExecutingAssembly();
  47. foreach (var type in assembly.GetTypes()) {
  48. if (typeof(IBsonSerializer).IsAssignableFrom(type) && type != typeof(IBsonSerializer)) {
  49. var registerSerializersInfo = type.GetMethod("RegisterSerializers", BindingFlags.Public | BindingFlags.Static);
  50. if (registerSerializersInfo != null) {
  51. registerSerializersInfo.Invoke(null, null);
  52. }
  53. }
  54. }
  55. }
  56. #endregion
  57. #region public methods
  58. public IBsonIdGenerator GetIdGenerator(
  59. Type type
  60. ) {
  61. // TODO: implement more IdGenerators?
  62. if (type == typeof(ObjectId)) {
  63. return new ObjectIdGenerator();
  64. } else if (type == typeof(Guid)) {
  65. return new GuidGenerator();
  66. } else {
  67. return null;
  68. }
  69. }
  70. public IBsonSerializer GetSerializer(
  71. Type type,
  72. object serializationOptions
  73. ) {
  74. if (type.IsArray) {
  75. return GenericArraySerializer.Singleton;
  76. }
  77. if (type.IsEnum) {
  78. return GeneralEnumSerializer.GetSerializer(serializationOptions);
  79. }
  80. if (
  81. type.IsGenericType &&
  82. type.GetGenericTypeDefinition() == typeof(Nullable<>)
  83. ) {
  84. return NullableTypeSerializer.Singleton;
  85. }
  86. if (
  87. type.IsClass &&
  88. !typeof(Array).IsAssignableFrom(type) &&
  89. !typeof(Enum).IsAssignableFrom(type)
  90. ) {
  91. return BsonClassMapSerializer.Singleton;
  92. }
  93. return null;
  94. }
  95. #endregion
  96. }
  97. }