UriSerializer.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace MongoDB.Bson.Serialization.Serializers
  17. {
  18. /// <summary>
  19. /// Represents a serializer for Uris.
  20. /// </summary>
  21. public class UriSerializer : ClassSerializerBase<Uri>
  22. {
  23. // constructors
  24. /// <summary>
  25. /// Initializes a new instance of the UriSerializer class.
  26. /// </summary>
  27. public UriSerializer()
  28. {
  29. }
  30. // public methods
  31. /// <summary>
  32. /// Deserializes a value.
  33. /// </summary>
  34. /// <param name="context">The deserialization context.</param>
  35. /// <param name="args">The deserialization args.</param>
  36. /// <returns>A deserialized value.</returns>
  37. protected override Uri DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  38. {
  39. var bsonReader = context.Reader;
  40. EnsureBsonTypeEquals(bsonReader, BsonType.String);
  41. return new Uri(bsonReader.ReadString(), UriKind.RelativeOrAbsolute);
  42. }
  43. /// <summary>
  44. /// Serializes a value.
  45. /// </summary>
  46. /// <param name="context">The serialization context.</param>
  47. /// <param name="args">The serialization args.</param>
  48. /// <param name="value">The object.</param>
  49. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, Uri value)
  50. {
  51. var bsonWriter = context.Writer;
  52. bsonWriter.WriteString(value.OriginalString);
  53. }
  54. }
  55. }