/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs

https://github.com/tmcgannon/mongo-csharp-driver · C# · 131 lines · 82 code · 7 blank · 42 comment · 8 complexity · effa54bdfebbff3891d7f10aeb111138 MD5 · raw file

  1. /* Copyright 2010-2013 10gen 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 System.IO;
  17. using System.Net;
  18. using System.Net.Sockets;
  19. using System.Text.RegularExpressions;
  20. using MongoDB.Bson.IO;
  21. namespace MongoDB.Bson.Serialization.Serializers
  22. {
  23. /// <summary>
  24. /// Represents a serializer for IPEndPoints.
  25. /// </summary>
  26. public class IPEndPointSerializer : BsonBaseSerializer
  27. {
  28. // private static fields
  29. private static IPEndPointSerializer __instance = new IPEndPointSerializer();
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the IPEndPointSerializer class.
  33. /// </summary>
  34. public IPEndPointSerializer()
  35. {
  36. }
  37. // public static properties
  38. /// <summary>
  39. /// Gets an instance of the IPEndPointSerializer class.
  40. /// </summary>
  41. [Obsolete("Use constructor instead.")]
  42. public static IPEndPointSerializer Instance
  43. {
  44. get { return __instance; }
  45. }
  46. // public methods
  47. /// <summary>
  48. /// Deserializes an object from a BsonReader.
  49. /// </summary>
  50. /// <param name="bsonReader">The BsonReader.</param>
  51. /// <param name="nominalType">The nominal type of the object.</param>
  52. /// <param name="actualType">The actual type of the object.</param>
  53. /// <param name="options">The serialization options.</param>
  54. /// <returns>An object.</returns>
  55. public override object Deserialize(
  56. BsonReader bsonReader,
  57. Type nominalType,
  58. Type actualType,
  59. IBsonSerializationOptions options)
  60. {
  61. VerifyTypes(nominalType, actualType, typeof(IPEndPoint));
  62. BsonType bsonType = bsonReader.GetCurrentBsonType();
  63. string message;
  64. switch (bsonType)
  65. {
  66. case BsonType.Null:
  67. bsonReader.ReadNull();
  68. return null;
  69. case BsonType.String:
  70. var stringValue = bsonReader.ReadString();
  71. var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$");
  72. if (match.Success)
  73. {
  74. IPAddress address;
  75. if (IPAddress.TryParse(match.Groups["address"].Value, out address))
  76. {
  77. int port;
  78. if (int.TryParse(match.Groups["port"].Value, out port))
  79. {
  80. return new IPEndPoint(address, port);
  81. }
  82. }
  83. }
  84. message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue);
  85. throw new FileFormatException(message);
  86. default:
  87. message = string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", bsonType);
  88. throw new FileFormatException(message);
  89. }
  90. }
  91. /// <summary>
  92. /// Serializes an object to a BsonWriter.
  93. /// </summary>
  94. /// <param name="bsonWriter">The BsonWriter.</param>
  95. /// <param name="nominalType">The nominal type.</param>
  96. /// <param name="value">The object.</param>
  97. /// <param name="options">The serialization options.</param>
  98. public override void Serialize(
  99. BsonWriter bsonWriter,
  100. Type nominalType,
  101. object value,
  102. IBsonSerializationOptions options)
  103. {
  104. if (value == null)
  105. {
  106. bsonWriter.WriteNull();
  107. }
  108. else
  109. {
  110. var endPoint = (IPEndPoint)value;
  111. string stringValue;
  112. if (endPoint.AddressFamily == AddressFamily.InterNetwork)
  113. {
  114. stringValue = string.Format("{0}:{1}", endPoint.Address, endPoint.Port); // IPv4
  115. }
  116. else
  117. {
  118. stringValue = string.Format("[{0}]:{1}", endPoint.Address, endPoint.Port); // IPv6
  119. }
  120. bsonWriter.WriteString(stringValue);
  121. }
  122. }
  123. }
  124. }