コード例 #1
0
        public object Deserialize(
            BsonReader bsonReader,
            Type classType
        )
        {
            // peek at the discriminator (if present) to see what class to create an instance for
            var discriminator = bsonReader.FindString("_t");
            if (discriminator != null) {
                var actualType = BsonClassMap.LookupTypeByDiscriminator(classType, discriminator);
                if (!classType.IsAssignableFrom(actualType)) {
                    string message = string.Format("Actual type {0} is not assignable to expected type {1}", actualType.FullName, classType.FullName);
                    throw new FileFormatException(message);
                }
                classType = actualType;
            }
            var classMap = BsonClassMap.LookupClassMap(classType);
            if (classMap.IsAnonymous) {
                throw new InvalidOperationException("Anonymous classes cannot be deserialized");
            }
            var obj = Activator.CreateInstance(classType);

            var missingElementPropertyMaps = new List<BsonPropertyMap>(classMap.PropertyMaps); // make a copy!
            bsonReader.ReadStartDocument();
            BsonType bsonType;
            string elementName;
            while (bsonReader.HasElement(out bsonType, out elementName)) {
                if (elementName == "_t") {
                    bsonReader.ReadString("_t"); // skip over discriminator
                    continue;
                }

                var propertyMap = classMap.GetPropertyMapForElement(elementName);
                if (propertyMap != null) {
                    propertyMap.PropertySerializer.DeserializeProperty(bsonReader, obj, propertyMap);
                    missingElementPropertyMaps.Remove(propertyMap);
                } else {
                    if (classMap.IgnoreExtraElements) {
                        bsonReader.SkipElement();
                    } else {
                        string message = string.Format("Unexpected element: {0}", elementName);
                        throw new FileFormatException(message);
                    }
                }
            }
            bsonReader.ReadEndDocument();

            foreach (var propertyMap in missingElementPropertyMaps) {
                if (propertyMap.IsRequired) {
                    string message = string.Format("Required element is missing: {0}", propertyMap.ElementName);
                    throw new BsonSerializationException(message);
                }

                if (propertyMap.HasDefaultValue) {
                    propertyMap.ApplyDefaultValue(obj);
                }
            }

            return obj;
        }
コード例 #2
0
 public object Deserialize(
     BsonReader bsonReader,
     Type type
 )
 {
     var obj = Activator.CreateInstance(type);
     ((IBsonSerializable) obj).Deserialize(bsonReader);
     return obj;
 }
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var value = bsonReader.ReadBoolean(propertyMap.ElementName);
     propertyMap.Setter(obj, value);
 }
コード例 #4
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     BsonType bsonType = bsonReader.PeekBsonType();
     object value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         value = BsonSerializer.Deserialize(bsonReader, propertyMap.PropertyInfo.PropertyType);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #5
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonArray value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         bsonReader.ReadArrayName(propertyMap.ElementName);
         value = BsonArray.ReadFrom(bsonReader);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #6
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     BsonType bsonType = bsonReader.PeekBsonType();
     byte value;
     if (bsonType == BsonType.Int32) {
         value = (byte) bsonReader.ReadInt32(propertyMap.ElementName);
     } else if (bsonType == BsonType.Document) {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         bsonReader.VerifyString("_t", typeof(byte).FullName);
         value = (byte) bsonReader.ReadInt32("v");
         bsonReader.ReadEndDocument();
     } else {
         throw new FileFormatException("Element is not valid System.Byte");
     }
     propertyMap.Setter(obj, value);
 }
コード例 #7
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonInt64 value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         value = BsonInt64.Create(bsonReader.ReadInt64(propertyMap.ElementName));
     }
     propertyMap.Setter(obj, value);
 }
コード例 #8
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     throw new InvalidOperationException("BsonDocumentWrappers cannot be deserialized");
 }
コード例 #9
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonValue value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         BsonElement element = BsonElement.ReadFrom(bsonReader, propertyMap.ElementName);
         value = element.Value;
     }
     propertyMap.Setter(obj, value);
 }
コード例 #10
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonBinaryData value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         byte[] bytes;
         BsonBinarySubType subType;
         bsonReader.ReadBinaryData(propertyMap.ElementName, out bytes, out subType);
         value = new BsonBinaryData(bytes, subType);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #11
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     bsonReader.ReadDocumentName(propertyMap.ElementName);
     bsonReader.ReadStartDocument();
     bsonReader.VerifyString("_t", typeof(Decimal).FullName);
     var value = XmlConvert.ToDecimal(bsonReader.ReadString("v"));
     bsonReader.ReadEndDocument();
     propertyMap.Setter(obj, value);
 }
コード例 #12
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonNull value;
     if (bsonType == BsonType.Document) {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         bsonReader.VerifyString("$null", "c#null");
         bsonReader.ReadEndDocument();
         value = null;
     } else {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = Bson.Null;
     }
     propertyMap.Setter(obj, value);
 }
コード例 #13
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     bsonReader.ReadDocumentName(propertyMap.ElementName);
     bsonReader.ReadStartDocument();
     bsonReader.VerifyString("_t", typeof(CultureInfo).FullName);
     var value = new CultureInfo(bsonReader.ReadString("v"));
     bsonReader.ReadEndDocument();
     propertyMap.Setter(obj, value);
 }
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     int timestamp;
     long machinePidIncrement;
     bsonReader.ReadObjectId(propertyMap.ElementName, out timestamp, out machinePidIncrement);
     var value = new ObjectId(timestamp, machinePidIncrement);
     propertyMap.Setter(obj, value);
 }
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     byte[] bytes;
     BsonBinarySubType subType;
     bsonReader.ReadBinaryData(propertyMap.ElementName, out bytes, out subType);
     if (bytes.Length != 16) {
         throw new FileFormatException("BinaryData length is not 16");
     }
     if (subType != BsonBinarySubType.Uuid) {
         throw new FileFormatException("BinaryData sub type is not Uuid");
     }
     var value = new Guid(bytes);
     propertyMap.Setter(obj, value);
 }
コード例 #16
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     BsonType bsonType = bsonReader.PeekBsonType();
     TimeSpan value;
     if (bsonType == BsonType.Int64) {
         value = new TimeSpan(bsonReader.ReadInt64(propertyMap.ElementName));
     } else if (bsonType == BsonType.Document) {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         bsonReader.VerifyString("_t", typeof(TimeSpan).FullName);
         value = TimeSpan.Parse(bsonReader.ReadString("v"));
         bsonReader.ReadEndDocument();
     } else {
         throw new FileFormatException("Element is not valid System.TimeSpan");
     }
     propertyMap.Setter(obj, value);
 }
コード例 #17
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     BsonType bsonType = bsonReader.PeekBsonType();
     double doubleValue;
     if (bsonType == BsonType.Double) {
         doubleValue = bsonReader.ReadDouble(propertyMap.ElementName);
     } else if (bsonType == BsonType.Document) {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         bsonReader.VerifyString("_t", typeof(float).FullName);
         doubleValue = bsonReader.ReadDouble("v");
         bsonReader.ReadEndDocument();
     } else {
         throw new FileFormatException("Element is not valid System.Single");
     }
     var value = doubleValue == double.MinValue ? float.MinValue : doubleValue == double.MaxValue ? float.MaxValue : (float) doubleValue;
     propertyMap.Setter(obj, value);
 }
コード例 #18
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonJavaScript value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         value = new BsonJavaScript(bsonReader.ReadJavaScript(propertyMap.ElementName));
     }
     propertyMap.Setter(obj, value);
 }
コード例 #19
0
 public void Deserialize(
     BsonReader bsonReader
 )
 {
     throw new InvalidOperationException("Deserialize not valid for BsonDocumentWrapper");
 }
コード例 #20
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonJavaScriptWithScope value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         var code = bsonReader.ReadJavaScriptWithScope(propertyMap.ElementName);
         var scope = BsonDocument.ReadFrom(bsonReader);
         value = new BsonJavaScriptWithScope(code, scope);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #21
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonRegularExpression value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         string pattern, options;
         bsonReader.ReadRegularExpression(propertyMap.ElementName, out pattern, out options);
         value = new BsonRegularExpression(pattern, options);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #22
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var bsonType = bsonReader.PeekBsonType();
     BsonObjectId value;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull(propertyMap.ElementName);
         value = null;
     } else {
         int timestamp;
         long machinePidIncrement;
         bsonReader.ReadObjectId(propertyMap.ElementName, out timestamp, out machinePidIncrement);
         value = new BsonObjectId(timestamp, machinePidIncrement);
     }
     propertyMap.Setter(obj, value);
 }
コード例 #23
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     BsonType bsonType = bsonReader.PeekBsonType();
     DateTimeOffset value;
     if (bsonType == BsonType.Array) {
         bsonReader.ReadArrayName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         var dateTime = new DateTime(bsonReader.ReadInt64("0"));
         var offset = new TimeSpan(bsonReader.ReadInt64("1"));
         bsonReader.ReadEndDocument();
         value = new DateTimeOffset(dateTime, offset);
     } else if (bsonType == BsonType.Document) {
         bsonReader.ReadDocumentName(propertyMap.ElementName);
         bsonReader.ReadStartDocument();
         bsonReader.VerifyString("_t", typeof(DateTimeOffset).FullName);
         var dateTime = DateTime.Parse(bsonReader.ReadString("dt")); // Kind = DateTimeKind.Unspecified
         var offset = TimeSpan.Parse(bsonReader.ReadString("o"));
         bsonReader.ReadEndDocument();
         value = new DateTimeOffset(dateTime, offset);
     } else {
         throw new FileFormatException("Element is not valid System.DateTimeOffset");
     }
     propertyMap.Setter(obj, value);
 }
コード例 #24
0
 void IBsonSerializable.Deserialize(
     BsonReader bsonReader
 )
 {
     throw new InvalidOperationException("Deserialize is not supported for SortByBuilder");
 }
コード例 #25
0
 public void DeserializeProperty(
     BsonReader bsonReader,
     object obj,
     BsonPropertyMap propertyMap
 )
 {
     var employee = (Employee) obj;
     employee.DateOfBirth = DateTime.Parse(bsonReader.ReadString(propertyMap.ElementName));
 }