// 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 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 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) { var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null return(null); } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); return(null); } var serializer = BsonSerializer.LookupSerializer(actualType); return(serializer.Deserialize(bsonReader, nominalType, actualType, options)); }
// 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(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, // ignored IBsonSerializationOptions options) { var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: return (BsonValue)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), options); case BsonType.Binary: return (BsonValue)BsonBinaryDataSerializer.Instance.Deserialize(bsonReader, typeof(BsonBinaryData), options); case BsonType.Boolean: return (BsonValue)BsonBooleanSerializer.Instance.Deserialize(bsonReader, typeof(BsonBoolean), options); case BsonType.DateTime: return (BsonValue)BsonDateTimeSerializer.Instance.Deserialize(bsonReader, typeof(BsonDateTime), options); case BsonType.Document: return (BsonValue)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), options); case BsonType.Double: return (BsonValue)BsonDoubleSerializer.Instance.Deserialize(bsonReader, typeof(BsonDouble), options); case BsonType.Int32: return (BsonValue)BsonInt32Serializer.Instance.Deserialize(bsonReader, typeof(BsonInt32), options); case BsonType.Int64: return (BsonValue)BsonInt64Serializer.Instance.Deserialize(bsonReader, typeof(BsonInt64), options); case BsonType.JavaScript: return (BsonValue)BsonJavaScriptSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScript), options); case BsonType.JavaScriptWithScope: return (BsonValue)BsonJavaScriptWithScopeSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScriptWithScope), options); case BsonType.MaxKey: return (BsonValue)BsonMaxKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMaxKey), options); case BsonType.MinKey: return (BsonValue)BsonMinKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMinKey), options); case BsonType.Null: return (BsonValue)BsonNullSerializer.Instance.Deserialize(bsonReader, typeof(BsonNull), options); case BsonType.ObjectId: return (BsonValue)BsonObjectIdSerializer.Instance.Deserialize(bsonReader, typeof(BsonObjectId), options); case BsonType.RegularExpression: return (BsonValue)BsonRegularExpressionSerializer.Instance.Deserialize(bsonReader, typeof(BsonRegularExpression), options); case BsonType.String: return (BsonValue)BsonStringSerializer.Instance.Deserialize(bsonReader, typeof(BsonString), options); case BsonType.Symbol: return (BsonValue)BsonSymbolSerializer.Instance.Deserialize(bsonReader, typeof(BsonSymbol), options); case BsonType.Timestamp: return (BsonValue)BsonTimestampSerializer.Instance.Deserialize(bsonReader, typeof(BsonTimestamp), options); case BsonType.Undefined: return (BsonValue)BsonUndefinedSerializer.Instance.Deserialize(bsonReader, typeof(BsonUndefined), options); default: var message = string.Format("Invalid BsonType {0}.", bsonType); throw new BsonInternalException(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); } }
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return(null); } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return(serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions)); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new Exception(message, ex); } }
// 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(BsonSymbol)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.String: return(BsonSymbol.Create(bsonReader.ReadString())); case BsonType.Symbol: return(BsonSymbol.Create(bsonReader.ReadSymbol())); default: var message = string.Format("Cannot deserialize BsonSymbol from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Gets the actual type of an object by reading the discriminator from a BsonReader. /// </summary> /// <param name="bsonReader">The reader.</param> /// <param name="nominalType">The nominal type.</param> /// <returns>The actual type.</returns> public Type GetActualType(BsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer) BsonSerializer.EnsureKnownTypesAreRegistered(nominalType); // we can skip looking for a discriminator if nominalType has no discriminated sub types if (BsonSerializer.IsTypeDiscriminated(nominalType)) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(_elementName)) { var context = BsonDeserializationContext.CreateRoot <BsonValue>(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); if (discriminator.IsBsonArray) { discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator } actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return(actualType); } } return(nominalType); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) { pb.Trace.WriteLine("ZStringArraySerializer.Deserialize()"); } VerifyTypes(nominalType, actualType, typeof(ZStringArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); //return new ZString(bsonReader.ReadString()); var array = new List <string>(); bsonType = bsonReader.ReadBsonType(); while (bsonType != BsonType.EndOfDocument) { if (bsonType != BsonType.String) { throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); } var value = bsonReader.ReadString(); array.Add(value); bsonType = bsonReader.ReadBsonType(); } bsonReader.ReadEndArray(); return(new ZStringArray(array.ToArray())); default: throw new PBException("error cannot deserialize ZStringArray from BsonType {0}.", bsonType); } }
// 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(BsonArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); var array = new BsonArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); array.Add(value); } bsonReader.ReadEndArray(); return(array); default: var message = string.Format("Cannot deserialize BsonArray 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(ushort)); var representationSerializationOptions = EnsureSerializationOptions <RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return(representationSerializationOptions.ToUInt16(bsonReader.ReadDouble())); case BsonType.Int32: return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt32())); case BsonType.Int64: return(representationSerializationOptions.ToUInt16(bsonReader.ReadInt64())); case BsonType.String: return(XmlConvert.ToUInt16(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize uInt16 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(LazyBsonArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: var slice = bsonReader.ReadRawBsonArray(); return(new LazyBsonArray(slice)); default: var message = string.Format("Cannot deserialize LazyBsonArray from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.GetCurrentBsonType() == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
/// <summary> /// Deserializes an Image from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the Image.</param> /// <param name="actualType">The actual type of the Image.</param> /// <param name="options">The serialization options.</param> /// <returns>An Image.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (nominalType != typeof(Image)) { var message = string.Format("Nominal type must be Image, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (actualType != typeof(Image)) { var message = string.Format("Actual type must be Image, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var message = string.Format("BsonType must be Null, not {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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartDocument(); DeserializeType(bsonReader, "link"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var href = bsonReader.ReadString("href"); string hrefType = null; if (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { hrefType = bsonReader.ReadString("type"); } bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return(new GeoJsonLinkedCoordinateReferenceSystem(href, hrefType)); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } var nvc = new NameValueCollection(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var key = (string)(new StringSerializer().Deserialize(bsonReader, typeof(string), options)); var val = (string)(new StringSerializer().Deserialize(bsonReader, typeof(string), options)); bsonReader.ReadEndArray(); nvc.Add(key, val); } bsonReader.ReadEndArray(); return(nvc); }
static object ReadObject(BsonReader bsonReader) //_120509_173140 keep consistent { switch (bsonReader.GetCurrentBsonType()) { case BsonType.Array: return(ReadArray(bsonReader)); // replacement case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime())); case BsonType.Document: return(ReadCustomObject(bsonReader)); // replacement case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader)); } }
// 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(BsonBinaryData)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; GuidRepresentation guidRepresentation; bsonReader.ReadBinaryData(out bytes, out subType, out guidRepresentation); return(new BsonBinaryData(bytes, subType, guidRepresentation)); default: var message = string.Format("Cannot deserialize BsonBinaryData from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
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).Add(offset)); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); ticks = bsonReader.ReadInt64("Ticks"); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset")); bsonReader.ReadEndDocument(); return(new DateTimeOffset(ticks, offset).Add(offset)); default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new ArgumentException(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="options">The serialization options.</param> /// <returns>An object.</returns> public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { VerifyNominalType(nominalType); if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType); var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); if (actualType != nominalType) { var serializer = BsonSerializer.LookupSerializer(actualType); if (serializer != this) { // in rare cases a concrete actualType might have a more specialized serializer return(serializer.Deserialize(bsonReader, nominalType, actualType, options)); } } return(Deserialize(bsonReader, nominalType, actualType, options)); } }
// 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) { 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: var instance = CreateInstance(actualType); var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); Type lastItemType = null; IBsonSerializer lastItemSerializer = null; bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var itemType = itemDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); IBsonSerializer itemSerializer; if (itemType == lastItemType) { itemSerializer = lastItemSerializer; } else { itemSerializer = BsonSerializer.LookupSerializer(itemType); lastItemType = itemType; lastItemSerializer = itemSerializer; } var item = itemSerializer.Deserialize(bsonReader, typeof(object), itemType, itemSerializationOptions); AddItem(instance, item); } bsonReader.ReadEndArray(); return(FinalizeResult(instance, actualType)); 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(byte)); var bsonType = bsonReader.GetCurrentBsonType(); byte value; var lostData = false; switch (bsonType) { case BsonType.Binary: byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); 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) { 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: var instance = CreateInstance(actualType); var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); Type lastItemType = null; IBsonSerializer lastItemSerializer = null; bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var itemType = itemDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); IBsonSerializer itemSerializer; if (itemType == lastItemType) { itemSerializer = lastItemSerializer; } else { itemSerializer = BsonSerializer.LookupSerializer(itemType); lastItemType = itemType; lastItemSerializer = itemSerializer; } var item = itemSerializer.Deserialize(bsonReader, typeof(object), itemType, itemSerializationOptions); AddItem(instance, item); } bsonReader.ReadEndArray(); return FinalizeResult(instance, actualType); 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 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 #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; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: return(new BitArray(bsonReader.ReadBytes())); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); 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: var 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(default(TInterface)); } else { return(_lazyImplementationSerializer.Value.Deserialize(bsonReader, nominalType, typeof(TImplementation), options)); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; object value = null; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); goto setvalue; } else if (memberMap.MemberTypeIsBsonValue) { if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null goto setvalue; } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); goto setvalue; } } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); setvalue: memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
/// <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(ZonedDateTime)); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.DateTime) { var millisecondsSinceEpoch = bsonReader.ReadDateTime(); return(new Instant(millisecondsSinceEpoch).InUtc()); } throw new InvalidOperationException(string.Format("Cannot deserialize ZonedDateTime from BsonType {0}.", bsonType)); }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var value = bsonReader.ReadString(); return(new EmailAddress(value)); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var value = bsonReader.ReadString(); return(new Card(Suit.FromShortName(value.Substring(0, 1)), Rank.FromString(value.Substring(1)))); } }
// 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(sbyte)); var bsonType = bsonReader.GetCurrentBsonType(); var lostData = false; sbyte value; switch (bsonType) { case BsonType.Binary: var bytes = bsonReader.ReadBytes(); if (bytes.Length != 1) { throw new FileFormatException("Binary data for SByte must be exactly one byte long."); } value = (sbyte)bytes[0]; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (sbyte)int32Value; lostData = (int)value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (sbyte)int64Value; lostData = (int)value != int64Value; break; case BsonType.String: var s = bsonReader.ReadString(); if (s.Length == 1) { s = "0" + s; } value = (sbyte)byte.Parse(s, NumberStyles.HexNumber); break; default: var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", 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="options">The serialization options.</param> /// <returns> /// An object. /// </returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var actualType = GetActualType(bsonReader); var actualTypeSerializer = BsonSerializer.LookupSerializer(actualType); return(actualTypeSerializer.Deserialize(bsonReader, nominalType, actualType, options)); } }
// 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)); var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset 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("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = 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 DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } 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; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: return new BitArray(bsonReader.ReadBytes()); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); 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: var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(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; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bytes = bsonReader.ReadBytes(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); bytes = bsonReader.ReadBytes("bitmap"); bsonReader.ReadEndDocument(); break; default: var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType); 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) { VerifyTypes(nominalType, actualType, typeof(BsonSymbol)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Symbol: return BsonSymbolTable.Lookup(bsonReader.ReadSymbol()); default: var message = string.Format("Cannot deserialize BsonSymbol 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(BsonMinKey)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.MinKey: bsonReader.ReadMinKey(); return BsonMinKey.Value; default: var message = string.Format("Cannot deserialize BsonMinKey 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(BsonJavaScript)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.JavaScript: var code = bsonReader.ReadJavaScript(); return new BsonJavaScript(code); default: var message = string.Format("Cannot deserialize BsonJavaScript 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(BsonDateTime)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.DateTime: var millisecondsSinceEpoch = bsonReader.ReadDateTime(); return new BsonDateTime(millisecondsSinceEpoch); default: var message = string.Format("Cannot deserialize BsonDateTime 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 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: 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 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="options">The serialization options.</param> /// <returns>An object.</returns> public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (nominalType != typeof(object)) { var message = string.Format("ObjectSerializer can only be used with nominal type System.Object, not type {0}.", nominalType.FullName); throw new InvalidOperationException(message); } var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.ReadBsonType() == BsonType.EndOfDocument) { bsonReader.ReadEndDocument(); return new object(); } else { bsonReader.ReturnToBookmark(bookmark); } } var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); if (actualType == typeof(object)) { var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is {0}.", bsonType); throw new FileFormatException(message); } var serializer = BsonSerializer.LookupSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, options); }
// 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: var binaryData = bsonReader.ReadBinaryData(); var bytes = binaryData.Bytes; var subType = binaryData.SubType; var guidRepresentation = binaryData.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 of type System.Drawing.Size 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(System.Drawing.Size)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Document: bsonReader.ReadStartDocument(); var width = bsonReader.ReadInt32("Width"); var height = bsonReader.ReadInt32("Height"); bsonReader.ReadEndDocument(); return new System.Drawing.Size(width, height); default: var message = string.Format("Cannot deserialize Size 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(LazyBsonDocument)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Document: var slice = bsonReader.ReadRawBsonDocument(); return new LazyBsonDocument(slice); default: var message = string.Format("Cannot deserialize LazyBsonDocument 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(BsonDocument)); var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Document: var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions; if (documentSerializationOptions == null) { message = string.Format( "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.", BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)), BsonUtils.GetFriendlyTypeName(options.GetType())); throw new BsonSerializationException(message); } bsonReader.ReadStartDocument(); var document = new BsonDocument(documentSerializationOptions.AllowDuplicateNames); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); document.Add(name, value); } bsonReader.ReadEndDocument(); return document; default: message = string.Format("Cannot deserialize BsonDocument from BsonType {0}.", bsonType); throw new FileFormatException(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) { var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null return null; } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); return null; } var serializer = BsonSerializer.LookupSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, options); }
// 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="options">The serialization options.</param> /// <returns>An object.</returns> public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { VerifyNominalType(nominalType); if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { var discriminatorConvention = _classMap.GetDiscriminatorConvention(); var actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); if (actualType != nominalType) { var serializer = BsonSerializer.LookupSerializer(actualType); if (serializer != this) { return serializer.Deserialize(bsonReader, nominalType, actualType, options); } } return Deserialize(bsonReader, nominalType, actualType, options); } }
/// <summary> /// Deserializes an Image from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the Image.</param> /// <param name="actualType">The actual type of the Image.</param> /// <param name="options">The serialization options.</param> /// <returns>An Image.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (nominalType != typeof(Image)) { var message = string.Format("Nominal type must be Image, not {0}.", nominalType.FullName); throw new ArgumentException(message, "nominalType"); } if (actualType != typeof(Image)) { var message = string.Format("Actual type must be Image, not {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { var message = string.Format("BsonType must be Null, not {0}.", bsonType); throw new FileFormatException(message); } }
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return null; } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new FileFormatException(message, ex); } }
/// <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 object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (actualType != typeof(object)) { var message = string.Format("ObjectSerializer can only be used with actual type System.Object, not type {0}.", actualType.FullName); throw new ArgumentException(message, "actualType"); } var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { bsonReader.ReadStartDocument(); if (bsonReader.ReadBsonType() == BsonType.EndOfDocument) { bsonReader.ReadEndDocument(); return new object(); } else { var message = string.Format("A document being deserialized to System.Object must be empty."); throw new FileFormatException(message); } } else { var message = string.Format("Cannot deserialize System.Object 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) { 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); } }
/// <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 object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyNominalType(nominalType); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { if (actualType != _classMap.ClassType) { var message = string.Format("BsonClassMapSerializer.Deserialize for type {0} was called with actualType {1}.", BsonUtils.GetFriendlyTypeName(_classMap.ClassType), BsonUtils.GetFriendlyTypeName(actualType)); throw new BsonSerializationException(message); } if (actualType.IsValueType) { var message = string.Format("Value class {0} cannot be deserialized.", actualType.FullName); throw new BsonSerializationException(message); } if (_classMap.IsAnonymous) { throw new InvalidOperationException("An anonymous class cannot be deserialized."); } if (bsonType != BsonType.Document) { var message = string.Format( "Expected a nested document representing the serialized form of a {0} value, but found a value of type {1} instead.", actualType.FullName, bsonType); throw new FileFormatException(message); } Dictionary<string, object> values = null; object obj = null; ISupportInitialize supportsInitialization = null; if (_classMap.HasCreatorMaps) { // for creator-based deserialization we first gather the values in a dictionary and then call a matching creator values = new Dictionary<string, object>(); } else { // for mutable classes we deserialize the values directly into the result object obj = _classMap.CreateInstance(); supportsInitialization = obj as ISupportInitialize; if (supportsInitialization != null) { supportsInitialization.BeginInit(); } } var discriminatorConvention = _classMap.GetDiscriminatorConvention(); var allMemberMaps = _classMap.AllMemberMaps; var extraElementsMemberMapIndex = _classMap.ExtraElementsMemberMapIndex; var memberMapBitArray = FastMemberMapHelper.GetBitArray(allMemberMaps.Count); bsonReader.ReadStartDocument(); var elementTrie = _classMap.ElementTrie; bool memberMapFound; int memberMapIndex; while (bsonReader.ReadBsonType(elementTrie, out memberMapFound, out memberMapIndex) != BsonType.EndOfDocument) { var elementName = bsonReader.ReadName(); if (memberMapFound) { var memberMap = allMemberMaps[memberMapIndex]; if (memberMapIndex != extraElementsMemberMapIndex) { if (obj != null) { if (memberMap.IsReadOnly) { bsonReader.SkipValue(); } else { var value = DeserializeMemberValue(bsonReader, memberMap); memberMap.Setter(obj, value); } } else { var value = DeserializeMemberValue(bsonReader, memberMap); values[elementName] = value; } } else { DeserializeExtraElement(bsonReader, obj, elementName, memberMap); } memberMapBitArray[memberMapIndex >> 5] |= 1U << (memberMapIndex & 31); } else { if (elementName == discriminatorConvention.ElementName) { bsonReader.SkipValue(); // skip over discriminator continue; } if (extraElementsMemberMapIndex >= 0) { DeserializeExtraElement(bsonReader, obj, elementName, _classMap.ExtraElementsMemberMap); memberMapBitArray[extraElementsMemberMapIndex >> 5] |= 1U << (extraElementsMemberMapIndex & 31); } else if (_classMap.IgnoreExtraElements) { bsonReader.SkipValue(); } else { //james.wei 针对/_id属性没有扩展映射的提示。 var message = string.Format("Element '{0}' does not match any field or property of class {1}.",elementName, _classMap.ClassType.FullName); throw new FileFormatException(message); } } } bsonReader.ReadEndDocument(); // check any members left over that we didn't have elements for (in blocks of 32 elements at a time) for (var bitArrayIndex = 0; bitArrayIndex < memberMapBitArray.Length; ++bitArrayIndex) { memberMapIndex = bitArrayIndex << 5; var memberMapBlock = ~memberMapBitArray[bitArrayIndex]; // notice that bits are flipped so 1's are now the missing elements // work through this memberMapBlock of 32 elements while (true) { // examine missing elements (memberMapBlock is shifted right as we work through the block) for (; (memberMapBlock & 1) != 0; ++memberMapIndex, memberMapBlock >>= 1) { var memberMap = allMemberMaps[memberMapIndex]; if (memberMap.IsReadOnly) { continue; } if (memberMap.IsRequired) { var fieldOrProperty = (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property"; var message = string.Format( "Required element '{0}' for {1} '{2}' of class {3} is missing.", memberMap.ElementName, fieldOrProperty, memberMap.MemberName, _classMap.ClassType.FullName); throw new FileFormatException(message); } if (obj != null) { memberMap.ApplyDefaultValue(obj); } else if (memberMap.IsDefaultValueSpecified && !memberMap.IsReadOnly) { values[memberMap.ElementName] = memberMap.DefaultValue; } } if (memberMapBlock == 0) { break; } // skip ahead to the next missing element var leastSignificantBit = FastMemberMapHelper.GetLeastSignificantBit(memberMapBlock); memberMapIndex += leastSignificantBit; memberMapBlock >>= leastSignificantBit; } } if (obj != null) { if (supportsInitialization != null) { supportsInitialization.EndInit(); } return obj; } else { return CreateInstanceUsingCreator(values); } } }