IBsonSerializerExtensions.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2010-2015 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. namespace MongoDB.Bson.Serialization
  17. {
  18. /// <summary>
  19. /// Extensions methods for IBsonSerializer.
  20. /// </summary>
  21. public static class IBsonSerializerExtensions
  22. {
  23. // methods
  24. /// <summary>
  25. /// Deserializes a value.
  26. /// </summary>
  27. /// <param name="serializer">The serializer.</param>
  28. /// <param name="context">The deserialization context.</param>
  29. /// <returns>A deserialized value.</returns>
  30. public static object Deserialize(this IBsonSerializer serializer, BsonDeserializationContext context)
  31. {
  32. var args = new BsonDeserializationArgs { NominalType = serializer.ValueType };
  33. return serializer.Deserialize(context, args);
  34. }
  35. /// <summary>
  36. /// Deserializes a value.
  37. /// </summary>
  38. /// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
  39. /// <param name="serializer">The serializer.</param>
  40. /// <param name="context">The deserialization context.</param>
  41. /// <returns>A deserialized value.</returns>
  42. public static TValue Deserialize<TValue>(this IBsonSerializer<TValue> serializer, BsonDeserializationContext context)
  43. {
  44. var args = new BsonDeserializationArgs { NominalType = serializer.ValueType };
  45. return serializer.Deserialize(context, args);
  46. }
  47. /// <summary>
  48. /// Serializes a value.
  49. /// </summary>
  50. /// <param name="serializer">The serializer.</param>
  51. /// <param name="context">The serialization context.</param>
  52. /// <param name="value">The value.</param>
  53. public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, object value)
  54. {
  55. var args = new BsonSerializationArgs { NominalType = serializer.ValueType };
  56. serializer.Serialize(context, args, value);
  57. }
  58. /// <summary>
  59. /// Serializes a value.
  60. /// </summary>
  61. /// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
  62. /// <param name="serializer">The serializer.</param>
  63. /// <param name="context">The serialization context.</param>
  64. /// <param name="value">The value.</param>
  65. public static void Serialize<TValue>(this IBsonSerializer<TValue> serializer, BsonSerializationContext context, TValue value)
  66. {
  67. var args = new BsonSerializationArgs { NominalType = serializer.ValueType };
  68. serializer.Serialize(context, args, value);
  69. }
  70. }
  71. }