BsonRepresentationAttribute.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 MongoDB.Bson.Serialization.Options;
  17. namespace MongoDB.Bson.Serialization.Attributes
  18. {
  19. /// <summary>
  20. /// Specifies the external representation and related options for this field or property.
  21. /// </summary>
  22. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
  23. public class BsonRepresentationAttribute : BsonSerializationOptionsAttribute
  24. {
  25. // private fields
  26. private BsonType _representation;
  27. private bool _allowOverflow;
  28. private bool _allowTruncation;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonRepresentationAttribute class.
  32. /// </summary>
  33. /// <param name="representation">The external representation.</param>
  34. public BsonRepresentationAttribute(BsonType representation)
  35. {
  36. _representation = representation;
  37. }
  38. // public properties
  39. /// <summary>
  40. /// Gets the external representation.
  41. /// </summary>
  42. public BsonType Representation
  43. {
  44. get { return _representation; }
  45. }
  46. /// <summary>
  47. /// Gets or sets whether to allow overflow.
  48. /// </summary>
  49. public bool AllowOverflow
  50. {
  51. get { return _allowOverflow; }
  52. set { _allowOverflow = value; }
  53. }
  54. /// <summary>
  55. /// Gets or sets whether to allow truncation.
  56. /// </summary>
  57. public bool AllowTruncation
  58. {
  59. get { return _allowTruncation; }
  60. set { _allowTruncation = value; }
  61. }
  62. // protected methods
  63. /// <summary>
  64. /// Reconfigures the specified serializer by applying this attribute to it.
  65. /// </summary>
  66. /// <param name="serializer">The serializer.</param>
  67. /// <returns>A reconfigured serializer.</returns>
  68. protected override IBsonSerializer Apply(IBsonSerializer serializer)
  69. {
  70. var representationConfigurable = serializer as IRepresentationConfigurable;
  71. if (representationConfigurable != null)
  72. {
  73. var reconfiguredSerializer = representationConfigurable.WithRepresentation(_representation);
  74. var converterConfigurable = reconfiguredSerializer as IRepresentationConverterConfigurable;
  75. if (converterConfigurable != null)
  76. {
  77. var converter = new RepresentationConverter(_allowOverflow, _allowTruncation);
  78. reconfiguredSerializer = converterConfigurable.WithConverter(converter);
  79. }
  80. return reconfiguredSerializer;
  81. }
  82. // for backward compatibility representations of Array and Document are mapped to DictionaryRepresentations if possible
  83. var dictionaryRepresentationConfigurable = serializer as IDictionaryRepresentationConfigurable;
  84. if (dictionaryRepresentationConfigurable != null)
  85. {
  86. if (_representation == BsonType.Array || _representation == BsonType.Document)
  87. {
  88. var dictionaryRepresentation = (_representation == BsonType.Array) ? DictionaryRepresentation.ArrayOfArrays: DictionaryRepresentation.Document;
  89. return dictionaryRepresentationConfigurable.WithDictionaryRepresentation(dictionaryRepresentation);
  90. }
  91. }
  92. return base.Apply(serializer);
  93. }
  94. }
  95. }