ByteArraySerializer.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 System.Globalization;
  17. using System.Text;
  18. namespace MongoDB.Bson.Serialization.Serializers
  19. {
  20. /// <summary>
  21. /// Represents a serializer for ByteArrays.
  22. /// </summary>
  23. public class ByteArraySerializer : SealedClassSerializerBase<byte[]>, IRepresentationConfigurable<ByteArraySerializer>
  24. {
  25. // private fields
  26. private readonly BsonType _representation;
  27. // constructors
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ByteArraySerializer"/> class.
  30. /// </summary>
  31. public ByteArraySerializer()
  32. : this(BsonType.Binary)
  33. {
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="ByteArraySerializer"/> class.
  37. /// </summary>
  38. /// <param name="representation">The representation.</param>
  39. public ByteArraySerializer(BsonType representation)
  40. {
  41. switch (representation)
  42. {
  43. case BsonType.Binary:
  44. case BsonType.String:
  45. break;
  46. default:
  47. var message = string.Format("{0} is not a valid representation for a ByteArraySerializer.", representation);
  48. throw new ArgumentException(message);
  49. }
  50. _representation = representation;
  51. }
  52. // public properties
  53. /// <summary>
  54. /// Gets the representation.
  55. /// </summary>
  56. /// <value>
  57. /// The representation.
  58. /// </value>
  59. public BsonType Representation
  60. {
  61. get { return _representation; }
  62. }
  63. // public methods
  64. #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
  65. /// <summary>
  66. /// Deserializes a value.
  67. /// </summary>
  68. /// <param name="context">The deserialization context.</param>
  69. /// <param name="args">The deserialization args.</param>
  70. /// <returns>A deserialized value.</returns>
  71. protected override byte[] DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
  72. {
  73. var bsonReader = context.Reader;
  74. BsonType bsonType = bsonReader.GetCurrentBsonType();
  75. switch (bsonType)
  76. {
  77. case BsonType.Binary:
  78. return bsonReader.ReadBytes();
  79. case BsonType.String:
  80. var s = bsonReader.ReadString();
  81. if ((s.Length % 2) != 0)
  82. {
  83. s = "0" + s; // prepend a zero to make length even
  84. }
  85. var bytes = new byte[s.Length / 2];
  86. for (int i = 0; i < s.Length; i += 2)
  87. {
  88. var hex = s.Substring(i, 2);
  89. var b = byte.Parse(hex, NumberStyles.HexNumber);
  90. bytes[i / 2] = b;
  91. }
  92. return bytes;
  93. default:
  94. throw CreateCannotDeserializeFromBsonTypeException(bsonType);
  95. }
  96. }
  97. #pragma warning restore 618
  98. /// <summary>
  99. /// Serializes a value.
  100. /// </summary>
  101. /// <param name="context">The serialization context.</param>
  102. /// <param name="args">The serialization args.</param>
  103. /// <param name="value">The object.</param>
  104. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, byte[] value)
  105. {
  106. var bsonWriter = context.Writer;
  107. switch (_representation)
  108. {
  109. case BsonType.Binary:
  110. bsonWriter.WriteBytes(value);
  111. break;
  112. case BsonType.String:
  113. bsonWriter.WriteString(BsonUtils.ToHexString(value));
  114. break;
  115. default:
  116. var message = string.Format("'{0}' is not a valid Byte[] representation.", _representation);
  117. throw new BsonSerializationException(message);
  118. }
  119. }
  120. /// <summary>
  121. /// Returns a serializer that has been reconfigured with the specified representation.
  122. /// </summary>
  123. /// <param name="representation">The representation.</param>
  124. /// <returns>The reconfigured serializer.</returns>
  125. public ByteArraySerializer WithRepresentation(BsonType representation)
  126. {
  127. if (representation == _representation)
  128. {
  129. return this;
  130. }
  131. else
  132. {
  133. return new ByteArraySerializer(representation);
  134. }
  135. }
  136. // explicit interface implementations
  137. IBsonSerializer IRepresentationConfigurable.WithRepresentation(BsonType representation)
  138. {
  139. return WithRepresentation(representation);
  140. }
  141. }
  142. }