ExpandoObjectSerializer.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Collections.Generic;
  16. using System.Dynamic;
  17. namespace MongoDB.Bson.Serialization.Serializers
  18. {
  19. /// <summary>
  20. /// Serializer for <see cref="ExpandoObject"/>.
  21. /// </summary>
  22. /// <remarks>
  23. /// The use of <see cref="ExpandoObject"/> will serialize any <see cref="List{Object}"/> without type information.
  24. /// To get the best experience out of using an <see cref="ExpandoObject"/>, any member wanting to be used
  25. /// as an array should use <see cref="List{Object}"/>.
  26. /// </remarks>
  27. public class ExpandoObjectSerializer : DynamicDocumentBaseSerializer<ExpandoObject>
  28. {
  29. // private fields
  30. private readonly IBsonSerializer<List<object>> _listSerializer;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="ExpandoObjectSerializer"/> class.
  33. /// </summary>
  34. public ExpandoObjectSerializer()
  35. {
  36. _listSerializer = BsonSerializer.LookupSerializer<List<object>>();
  37. }
  38. /// <summary>
  39. /// Configures the deserialization context.
  40. /// </summary>
  41. /// <param name="builder">The builder.</param>
  42. protected override void ConfigureDeserializationContext(BsonDeserializationContext.Builder builder)
  43. {
  44. builder.DynamicDocumentSerializer = this;
  45. builder.DynamicArraySerializer = _listSerializer;
  46. }
  47. /// <summary>
  48. /// Configures the serialization context.
  49. /// </summary>
  50. /// <param name="builder">The builder.</param>
  51. protected override void ConfigureSerializationContext(BsonSerializationContext.Builder builder)
  52. {
  53. builder.IsDynamicType = t => t == typeof(ExpandoObject) || t == typeof(List<object>);
  54. }
  55. /// <summary>
  56. /// Creates the document.
  57. /// </summary>
  58. /// <returns>
  59. /// A <see cref="ExpandoObject"/>.
  60. /// </returns>
  61. protected override ExpandoObject CreateDocument()
  62. {
  63. return new ExpandoObject();
  64. }
  65. /// <summary>
  66. /// Sets the value for the member.
  67. /// </summary>
  68. /// <param name="document">The document.</param>
  69. /// <param name="memberName">Name of the member.</param>
  70. /// <param name="value">The value.</param>
  71. protected override void SetValueForMember(ExpandoObject document, string memberName, object value)
  72. {
  73. ((IDictionary<string, object>)document)[memberName] = value;
  74. }
  75. /// <summary>
  76. /// Tries to get the value for a member. Returns true if the member should be serialized.
  77. /// </summary>
  78. /// <param name="value">The value.</param>
  79. /// <param name="memberName">Name of the member.</param>
  80. /// <param name="memberValue">The member value.</param>
  81. /// <returns><c>true</c> if the member should be serialized; otherwise <c>false</c>.</returns>
  82. protected override bool TryGetValueForMember(ExpandoObject value, string memberName, out object memberValue)
  83. {
  84. return ((IDictionary<string, object>)value).TryGetValue(memberName, out memberValue);
  85. }
  86. }
  87. }