// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(double)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return bsonReader.ReadDouble(); case BsonType.Int32: return representationSerializationOptions.ToDouble(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDouble(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDouble(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(CultureInfo)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Document: bsonReader.ReadStartDocument(); var name = bsonReader.ReadString("Name"); var useUserOverride = bsonReader.ReadBoolean("UseUserOverride"); bsonReader.ReadEndDocument(); return new CultureInfo(name, useUserOverride); case BsonType.String: return new CultureInfo(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize CultureInfo from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(bool)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Boolean: return bsonReader.ReadBoolean(); case BsonType.Double: return bsonReader.ReadDouble() != 0.0; case BsonType.Int32: return bsonReader.ReadInt32() != 0; case BsonType.Int64: return bsonReader.ReadInt64() != 0; case BsonType.Null: bsonReader.ReadNull(); return false; case BsonType.String: return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()); default: var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(ObjectId)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.ObjectId: int timestamp; int machine; short pid; int increment; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); return new ObjectId(timestamp, machine, pid, increment); case BsonType.String: return ObjectId.Parse(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize ObjectId from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return FromDouble(bsonReader.ReadDouble(), timeSpanSerializationOptions.Units); case BsonType.Int32: return FromInt32(bsonReader.ReadInt32(), timeSpanSerializationOptions.Units); case BsonType.Int64: return FromInt64(bsonReader.ReadInt64(), timeSpanSerializationOptions.Units); case BsonType.String: return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(IPAddress)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.String: var stringValue = bsonReader.ReadString(); IPAddress address; if (IPAddress.TryParse(stringValue, out address)) { return address; } message = string.Format("Invalid IPAddress value '{0}'.", stringValue); throw new FileFormatException(message); default: message = string.Format("Cannot deserialize IPAddress from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var typeName = bsonReader.ReadString(); return Type.GetType(typeName); }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { bsonReader.ReadStartDocument(); bsonReader.ReadName(); var typeName = bsonReader.ReadString(); bsonReader.ReadName(); var methodSignature = bsonReader.ReadString(); bsonReader.ReadEndDocument(); var type = Type.GetType(typeName); if (type != null) { var method = type.GetMethods().Where(m => GetMethodSignature(m) == methodSignature).SingleOrDefault(); return method; } return null; }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { // when entry is null, do not throw, just read null if (bsonReader.CurrentBsonType == MongoDB.Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } if (_failOnDeserialize) throw new InvalidOperationException(); return bsonReader.ReadString(); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, // ignored IBsonSerializationOptions options) { VerifyDeserializeType(nominalType); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return Enum.ToObject(nominalType, bsonReader.ReadInt32()); case BsonType.Int64: return Enum.ToObject(nominalType, bsonReader.ReadInt64()); case BsonType.Double: return Enum.ToObject(nominalType, (long)bsonReader.ReadDouble()); case BsonType.String: return Enum.Parse(nominalType, bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BsonString)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.String: return new BsonString(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize BsonString from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(char)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return (char)bsonReader.ReadInt32(); case BsonType.String: return (char)bsonReader.ReadString()[0]; default: var message = string.Format("Cannot deserialize Char from BsonType {0}.", bsonType); throw new Exception(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(ObjectId)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.ObjectId: return bsonReader.ReadObjectId(); case BsonType.String: return ObjectId.Parse(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize ObjectId from BsonType {0}.", bsonType); throw new Exception(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(Uri)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.String: return new Uri(bsonReader.ReadString(), UriKind.RelativeOrAbsolute); default: var message = string.Format("Cannot deserialize Uri from BsonType {0}.", bsonType); throw new Exception(message); } }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(byte[])); BsonType bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bytes = bsonReader.ReadBytes(); return bytes; case BsonType.String: var s = bsonReader.ReadString(); if ((s.Length % 2) != 0) { s = "0" + s; // prepend a zero to make length even } bytes = new byte[s.Length / 2]; for (int i = 0; i < s.Length; i += 2) { var hex = s.Substring(i, 2); var b = byte.Parse(hex, NumberStyles.HexNumber); bytes[i / 2] = b; } return bytes; default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(IPEndPoint)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.String: var stringValue = bsonReader.ReadString(); var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$"); if (match.Success) { IPAddress address; if (IPAddress.TryParse(match.Groups["address"].Value, out address)) { int port; if (int.TryParse(match.Groups["port"].Value, out port)) { return new IPEndPoint(address, port); } } } message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue); throw new FileFormatException(message); default: message = string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(decimal)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null); var bits = new int[4]; bits[0] = array[0].AsInt32; bits[1] = array[1].AsInt32; bits[2] = array[2].AsInt32; bits[3] = array[3].AsInt32; return new decimal(bits); case BsonType.Double: return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble()); case BsonType.Int32: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDecimal(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(string)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { switch (bsonType) { case BsonType.ObjectId: if (representationSerializationOptions.Representation == BsonType.ObjectId) { int timestamp, machine, increment; short pid; bsonReader.ReadObjectId(out timestamp, out machine, out pid, out increment); var objectId = new ObjectId(timestamp, machine, pid, increment); return objectId.ToString(); } else { goto default; } case BsonType.String: return bsonReader.ReadString(); case BsonType.Symbol: return bsonReader.ReadSymbol(); default: var message = string.Format("Cannot deserialize string from BsonType {0}.", bsonType); throw new FileFormatException(message); } } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(Guid)); var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; GuidRepresentation guidRepresentation; bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation); if (bytes.Length != 16) { message = string.Format("Expected length to be 16, not {0}.", bytes.Length); throw new FileFormatException(message); } if (subType != BsonBinarySubType.UuidStandard && subType != BsonBinarySubType.UuidLegacy) { message = string.Format("Expected binary sub type to be UuidStandard or UuidLegacy, not {0}.", subType); throw new FileFormatException(message); } if (guidRepresentation == GuidRepresentation.Unspecified) { throw new BsonSerializationException("GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified."); } return GuidConverter.FromBytes(bytes, guidRepresentation); case BsonType.String: return new Guid(bsonReader.ReadString()); default: message = string.Format("Cannot deserialize Guid from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTime)); var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTime value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = BsonDateTime.Create(bsonReader.ReadDateTime()).Value; break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (dateTimeSerializationOptions.DateOnly) { if (value.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime! } else { switch (dateTimeSerializationOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind); break; case DateTimeKind.Utc: value = BsonUtils.ToUniversalTime(value); break; } } return value; }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; byte[] bytes; BsonBinarySubType subType; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bsonReader.ReadBinaryData(out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type {0}.", subType); throw new FileFormatException(message); } return new BitArray(bytes); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); bsonReader.ReadBinaryData("Bytes", out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type {0}.", subType); throw new FileFormatException(message); } bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return bitArray; default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var versionString = bsonReader.ReadString(); return new MigrationVersion(versionString); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Array: bsonReader.ReadStartArray(); var queue = new Queue(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions); queue.Enqueue(element); } bsonReader.ReadEndArray(); return queue; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, actualType, options); bsonReader.ReadEndDocument(); return value; default: var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); BsonType bsonType = bsonReader.GetCurrentBsonType(); long ticks; TimeSpan offset; switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); ticks = bsonReader.ReadInt64(); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32()); bsonReader.ReadEndArray(); return new DateTimeOffset(ticks, offset); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value ticks = bsonReader.ReadInt64("Ticks"); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset")); bsonReader.ReadEndDocument(); return new DateTimeOffset(ticks, offset); case BsonType.String: return XmlConvert.ToDateTimeOffset(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new Exception(message); } }
// public methods /// <summary> /// Deserializes an Bitmap from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the Bitmap.</param> /// <param name="actualType">The actual type of the Bitmap.</param> /// <param name="options">The serialization options.</param> /// <returns>A Bitmap.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (nominalType != typeof(Image) && nominalType != typeof(Bitmap)) { var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (actualType != typeof(Bitmap)) { var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); byte[] bytes; BsonBinarySubType subType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bsonReader.ReadBinaryData(out bytes, out subType); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); bsonReader.ReadBinaryData("bitmap", out bytes, out subType); bsonReader.ReadEndDocument(); break; default: var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType); throw new FileFormatException(message); } if (subType != BsonBinarySubType.Binary) { var message = string.Format("Binary sub type must be Binary, not {0}.", subType); throw new FileFormatException(message); } var stream = new MemoryStream(bytes); return new Bitmap(stream); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var dictionarySerializationOptions = EnsureSerializationOptions(options); var dictionaryRepresentation = dictionarySerializationOptions.Representation; var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return value; } var dictionary = CreateInstance(actualType); var valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return dictionary; } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(actualType); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var keyValuePair = (KeyValuePair<object, object>)_keyValuePairSerializer.Deserialize( bsonReader, typeof(KeyValuePair<object, object>), keyValuePairSerializationOptions); dictionary.Add(keyValuePair.Key, keyValuePair.Value); } bsonReader.ReadEndArray(); return dictionary; } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.String) { return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) } else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks) { long ticks; switch (bsonType) { case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break; case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } return new TimeSpan(ticks); } else { double interval; switch (bsonType) { case BsonType.Double: interval = bsonReader.ReadDouble(); break; case BsonType.Int32: interval = bsonReader.ReadInt32(); break; case BsonType.Int64: interval = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } switch (timeSpanSerializationOptions.Units) { case TimeSpanUnits.Days: return TimeSpan.FromDays(interval); case TimeSpanUnits.Hours: return TimeSpan.FromHours(interval); case TimeSpanUnits.Minutes: return TimeSpan.FromMinutes(interval); case TimeSpanUnits.Seconds: return TimeSpan.FromSeconds(interval); case TimeSpanUnits.Milliseconds: return TimeSpan.FromMilliseconds(interval); case TimeSpanUnits.Nanoseconds: return TimeSpan.FromMilliseconds(interval / 1000.0); default: var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units); throw new BsonSerializationException(message); } } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(Version)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Document: bsonReader.ReadStartDocument(); int major = -1, minor = -1, build = -1, revision = -1; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "Major": major = bsonReader.ReadInt32(); break; case "Minor": minor = bsonReader.ReadInt32(); break; case "Build": build = bsonReader.ReadInt32(); break; case "Revision": revision = bsonReader.ReadInt32(); break; default: message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); if (major == -1) { message = string.Format("Version missing Major element."); throw new FileFormatException(message); } else if (minor == -1) { message = string.Format("Version missing Minor element."); throw new FileFormatException(message); } else if (build == -1) { return new Version(major, minor); } else if (revision == -1) { return new Version(major, minor, build); } else { return new Version(major, minor, build, revision); } case BsonType.String: return new Version(bsonReader.ReadString()); default: message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(byte)); var bsonType = bsonReader.GetCurrentBsonType(); byte value; var lostData = false; switch (bsonType) { case BsonType.Binary: var bytes = bsonReader.ReadBytes(); if (bytes.Length != 1) { throw new FileFormatException("Binary data for Byte must be exactly one byte long."); } value = bytes[0]; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (byte)int32Value; lostData = (int)value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (byte)int64Value; lostData = (int)value != int64Value; break; case BsonType.String: var s = bsonReader.ReadString(); if (s.Length == 1) { s = "0" + s; } value = byte.Parse(s, NumberStyles.HexNumber); break; default: var message = string.Format("Cannot deserialize Byte from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to Byte.", bsonType); throw new FileFormatException(message); } return value; }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BsonDateTime)); var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { long? millisecondsSinceEpoch = null; long? ticks = null; switch (bsonType) { case BsonType.DateTime: millisecondsSinceEpoch = bsonReader.ReadDateTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime"); bsonReader.ReadName("Ticks"); var ticksValue = BsonValue.ReadFrom(bsonReader); if (!ticksValue.IsBsonUndefined) { ticks = ticksValue.ToInt64(); } bsonReader.ReadEndDocument(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono DateTime dateTime; if (dateTimeSerializationOptions.DateOnly) { dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", }; dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } ticks = dateTime.Ticks; break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } BsonDateTime bsonDateTime; if (ticks.HasValue) { bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc)); } else { bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value); } if (dateTimeSerializationOptions.DateOnly) { var dateTime = bsonDateTime.Value; if (dateTime.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeSerializationOptions.Kind)); // not ToLocalTime or ToUniversalTime! } else { if (bsonDateTime.IsValidDateTime) { var dateTime = bsonDateTime.Value; switch (dateTimeSerializationOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(BsonUtils.ToLocalTime(dateTime), dateTimeSerializationOptions.Kind); break; case DateTimeKind.Utc: dateTime = BsonUtils.ToUniversalTime(dateTime); break; } bsonDateTime = BsonDateTime.Create(dateTime); } else { if (dateTimeSerializationOptions.Kind != DateTimeKind.Utc) { throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime."); } } } return bsonDateTime; } }