IBsonSerializerExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.IO;
  17. namespace MongoDB.Bson.Serialization
  18. {
  19. /// <summary>
  20. /// Extensions methods for IBsonSerializer.
  21. /// </summary>
  22. public static class IBsonSerializerExtensions
  23. {
  24. // methods
  25. /// <summary>
  26. /// Deserializes a value.
  27. /// </summary>
  28. /// <param name="serializer">The serializer.</param>
  29. /// <param name="context">The deserialization context.</param>
  30. /// <returns>A deserialized value.</returns>
  31. public static object Deserialize(this IBsonSerializer serializer, BsonDeserializationContext context)
  32. {
  33. var args = new BsonDeserializationArgs { NominalType = serializer.ValueType };
  34. return serializer.Deserialize(context, args);
  35. }
  36. /// <summary>
  37. /// Deserializes a value.
  38. /// </summary>
  39. /// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
  40. /// <param name="serializer">The serializer.</param>
  41. /// <param name="context">The deserialization context.</param>
  42. /// <returns>A deserialized value.</returns>
  43. public static TValue Deserialize<TValue>(this IBsonSerializer<TValue> serializer, BsonDeserializationContext context)
  44. {
  45. var args = new BsonDeserializationArgs { NominalType = serializer.ValueType };
  46. return serializer.Deserialize(context, args);
  47. }
  48. /// <summary>
  49. /// Serializes a value.
  50. /// </summary>
  51. /// <param name="serializer">The serializer.</param>
  52. /// <param name="context">The serialization context.</param>
  53. /// <param name="value">The value.</param>
  54. public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, object value)
  55. {
  56. var args = new BsonSerializationArgs { NominalType = serializer.ValueType };
  57. serializer.Serialize(context, args, value);
  58. }
  59. /// <summary>
  60. /// Serializes a value.
  61. /// </summary>
  62. /// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
  63. /// <param name="serializer">The serializer.</param>
  64. /// <param name="context">The serialization context.</param>
  65. /// <param name="value">The value.</param>
  66. public static void Serialize<TValue>(this IBsonSerializer<TValue> serializer, BsonSerializationContext context, TValue value)
  67. {
  68. var args = new BsonSerializationArgs { NominalType = serializer.ValueType };
  69. serializer.Serialize(context, args, value);
  70. }
  71. /// <summary>
  72. /// Converts a value to a BsonValue by serializing it.
  73. /// </summary>
  74. /// <param name="serializer">The serializer.</param>
  75. /// <param name="value">The value.</param>
  76. /// <returns>The serialized value.</returns>
  77. public static BsonValue ToBsonValue(this IBsonSerializer serializer, object value)
  78. {
  79. var document = new BsonDocument();
  80. using (var writer = new BsonDocumentWriter(document))
  81. {
  82. var context = BsonSerializationContext.CreateRoot(writer);
  83. writer.WriteStartDocument();
  84. writer.WriteName("x");
  85. serializer.Serialize(context, value);
  86. writer.WriteEndDocument();
  87. }
  88. return document[0];
  89. }
  90. /// <summary>
  91. /// Converts a value to a BsonValue by serializing it.
  92. /// </summary>
  93. /// <typeparam name="TValue">The type of the value.</typeparam>
  94. /// <param name="serializer">The serializer.</param>
  95. /// <param name="value">The value.</param>
  96. /// <returns>The serialized value.</returns>
  97. public static BsonValue ToBsonValue<TValue>(this IBsonSerializer<TValue> serializer, TValue value)
  98. {
  99. var document = new BsonDocument();
  100. using (var writer = new BsonDocumentWriter(document))
  101. {
  102. var context = BsonSerializationContext.CreateRoot(writer);
  103. writer.WriteStartDocument();
  104. writer.WriteName("x");
  105. serializer.Serialize(context, value);
  106. writer.WriteEndDocument();
  107. }
  108. return document[0];
  109. }
  110. }
  111. }