public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value = null;
            if (bsonReader.CurrentBsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
            }
            else
            {
                bsonReader.ReadStartArray();
                var idList = new List<ObjectId>();
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var id = (ObjectId)BsonSerializer.Deserialize(bsonReader, typeof(ObjectId));
                    idList.Add(id);
                }
                bsonReader.ReadEndArray();

                if (idList.Count > 0)
                {
                    var cursor = MongoDbProvider.Database.GetCollection(DocumentType, DocumentType.Name)
                        .FindAs(DocumentType, Query.In("_id", BsonArray.Create(idList)));

                    var documents = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(DocumentType));
                    foreach (var document in cursor)
                    {
                        documents.Add(document);
                    }
                    value = documents;
                }
            }
            return value;
        }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(IPAddress));

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            string message;
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return null;
                case BsonType.String:
                    var stringValue = bsonReader.ReadString();
                    IPAddress address;
                    if (IPAddress.TryParse(stringValue, out address))
                    {
                        return address;
                    }
                    message = string.Format("Invalid IPAddress value '{0}'.", stringValue);
                    throw new FileFormatException(message);
                default:
                    message = string.Format("Cannot deserialize IPAddress from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
        // public 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);
            }
        }
        /// <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 (nominalType != typeof(object)) {
                var message = string.Format("ObjectSerializer called for nominal type {0}.", nominalType.FullName);
                throw new InvalidOperationException(message);
            }

            var bsonType = bsonReader.CurrentBsonType;
            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 = BsonDefaultSerializer.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, null);
        }
        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)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options);
                var val = (string)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options);
                bsonReader.ReadEndArray();
                nvc.Add(key, val);
            }
            bsonReader.ReadEndArray();

            return nvc;
        }
        // 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(BsonNull));

            var bsonType = bsonReader.GetCurrentBsonType();
            string message;
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return BsonNull.Value;
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    var name = bsonReader.ReadName();
                    if (name == "_csharpnull" || name == "$csharpnull")
                    {
                        var csharpNull = bsonReader.ReadBoolean();
                        bsonReader.ReadEndDocument();
                        return csharpNull ? null : BsonNull.Value;
                    }
                    else
                    {
                        message = string.Format("Unexpected element name while deserializing a BsonNull: {0}.", name);
                        throw new FileFormatException(message);
                    }
                default:
                    message = string.Format("Cannot deserialize BsonNull 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 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.CurrentBsonType;
     if (bsonType == BsonType.Null)
     {
         bsonReader.ReadNull();
         return null;
     }
     else if (bsonType == BsonType.Array)
     {
         bsonReader.ReadStartArray();
         var list = new ArrayList();
         var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
         {
             var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
             var serializer = BsonSerializer.LookupSerializer(elementType);
             var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null);
             list.Add(element);
         }
         bsonReader.ReadEndArray();
         return list;
     }
     else
     {
         var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType);
         throw new FileFormatException(message);
     }
 }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(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)
        {
            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 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.CurrentBsonType == 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
#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.CurrentBsonType;
            BitArray bitArray;
            byte[] bytes;
            BsonBinarySubType subType;
            string message;
            switch (bsonType)
            {
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return null;
                case BsonType.Binary:
                    bsonReader.ReadBinaryData(out bytes, out subType);
                    if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
                    {
                        message = string.Format("Invalid Binary sub type {0}.", subType);
                        throw new FileFormatException(message);
                    }
                    return new BitArray(bytes);
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    var length = bsonReader.ReadInt32("Length");
                    bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
                    if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
                    {
                        message = string.Format("Invalid Binary sub type {0}.", subType);
                        throw new FileFormatException(message);
                    }
                    bsonReader.ReadEndDocument();
                    bitArray = new BitArray(bytes);
                    bitArray.Length = length;
                    return bitArray;
                case BsonType.String:
                    var s = bsonReader.ReadString();
                    bitArray = new BitArray(s.Length);
                    for (int i = 0; i < s.Length; i++)
                    {
                        var c = s[i];
                        switch (c)
                        {
                            case '0':
                                break;
                            case '1':
                                bitArray[i] = true;
                                break;
                            default:
                                throw new FileFormatException("String value is not a valid BitArray.");
                        }
                    }
                    return bitArray;
                default:
                    message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
        }
        // public 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 Exception(message);
            }
        }
Esempio n. 14
0
		public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
		{
			if (bsonReader.CurrentBsonType == BsonType.Null)
			{
				bsonReader.ReadNull();
				return null;
			}

			var id = (ObjectId)ObjectIdSerializer.Instance.Deserialize(bsonReader, typeof(ObjectId), typeof(ObjectId), options);
			return new Attachment(id);
		}
 /// <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
 ) {
     var bsonType = bsonReader.CurrentBsonType;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull();
         return null;
     } else {
         return BsonArray.ReadFrom(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, // ignored
     IBsonSerializationOptions options)
 {
     var bsonType = bsonReader.GetCurrentBsonType();
     if (bsonType == BsonType.Null)
     {
         bsonReader.ReadNull();
         return null;
     }
     else
     {
         return BsonValue.ReadFrom(bsonReader);
     }
 }
 // public methods
 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);
     }
 }
        protected double? ReadNullableDouble(BsonReader reader,string name)
        {
            reader.ReadName(name);

            var type = reader.GetCurrentBsonType();

            if ( type == BsonType.Null)
            {
                reader.ReadNull();
                return null;
            }

            return reader.ReadDouble();
        }
 public override object Deserialize(
     BsonReader bsonReader,
     Type nominalType,
     IBsonSerializationOptions options
 )
 {
     var bsonType = bsonReader.CurrentBsonType;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull();
         return null;
     } else if (bsonType == BsonType.Document) {
         var dictionary = CreateInstance(nominalType);
         bsonReader.ReadStartDocument();
         var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
             var key = bsonReader.ReadName();
             var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
             var valueSerializer = BsonSerializer.LookupSerializer(valueType);
             var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null);
             dictionary.Add(key, value);
         }
         bsonReader.ReadEndDocument();
         return dictionary;
     } else if (bsonType == BsonType.Array) {
         var dictionary = CreateInstance(nominalType);
         bsonReader.ReadStartArray();
         var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object));
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) {
             bsonReader.SkipName();
             bsonReader.ReadStartArray();
             bsonReader.ReadBsonType();
             bsonReader.SkipName();
             var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
             var keySerializer = BsonSerializer.LookupSerializer(keyType);
             var key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null);
             bsonReader.ReadBsonType();
             bsonReader.SkipName();
             var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
             var valueSerializer = BsonSerializer.LookupSerializer(valueType);
             var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null);
             bsonReader.ReadEndArray();
             dictionary.Add(key, value);
         }
         bsonReader.ReadEndArray();
         return dictionary;
     } else {
         var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType);
         throw new FileFormatException(message);
     }
 }
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            // when entry is null, do not throw, just read null
            if (bsonReader.CurrentBsonType == MongoDB.Bson.BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }

            if (_failOnDeserialize)
                throw new InvalidOperationException();

            return bsonReader.ReadString();
        }
 public override object Deserialize(
     BsonReader bsonReader,
     Type nominalType
 )
 {
     var bsonType = bsonReader.CurrentBsonType;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull();
         return null;
     } else {
         Type underlyingType = Nullable.GetUnderlyingType(nominalType);
         return BsonSerializer.Deserialize(bsonReader, underlyingType);
     }
 }
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value = null;

            if (bsonReader.CurrentBsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
            }
            else
            {
                var id = BsonValue.ReadFrom(bsonReader).AsObjectId;
                value = MongoDbProvider.Database.GetCollection(DocumentType, DocumentType.Name).FindOneAs(DocumentType, Query.EQ("_id", id));
            }
            return value;
        }
Esempio n. 23
0
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(WebImage));

            var bsonType = bsonReader.GetCurrentBsonType();
            switch (bsonType)
            {
                case BsonType.String:
                    return new WebImage(bsonReader.ReadString());
                case BsonType.Null:
                    bsonReader.ReadNull();
                    return new WebImage(null);
                default:
                    throw new PBException("error cannot deserialize UrlImage from BsonType {0}.", bsonType);
            }
        }
Esempio n. 24
0
 public override object Deserialize(
  BsonReader bsonReader,
  Type nominalType,
  Type actualType,
  IBsonSerializationOptions options)
 {
     var bsonType = bsonReader.GetCurrentBsonType();
     if (bsonType == BsonType.Null)
     {
         bsonReader.ReadNull();
         return null;
     }
     else if (bsonType == BsonType.Document)
     {
         var dictionary = CreateInstance(nominalType);
         bsonReader.ReadStartDocument();
         var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object));
         while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
         {
             var key = bsonReader.ReadName();
             //TODO:对键进行字符串留用.Intern.
             if (key.Length < 16)
             {
                 key = String.Intern(key);
             }
             var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object));
             var valueSerializer = BsonSerializer.LookupSerializer(valueType);
             var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null);
             //dictionary.Add(key, value);
             if (dictionary is IDictionary<string, object>)
             {
                 ((IDictionary<string, object>)dictionary).Add(key, value);
             }
             else if (dictionary is IDictionary)
             {
                 ((IDictionary)dictionary).Add(key, value);
             }
         }
         bsonReader.ReadEndDocument();
         return dictionary;
     }
     else
     {
         var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType);
         throw new FileFormatException(message);
     }
 }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>
        /// An object.
        /// </returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                bsonReader.ReadStartArray();
                var easting = bsonReader.ReadDouble();
                var northing = bsonReader.ReadDouble();
                bsonReader.ReadEndArray();

                return new GeoJson2DProjectedCoordinates(easting, northing);
            }
        }
        // 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.ReadStartArray();
                var longitude = bsonReader.ReadDouble();
                var latitude = bsonReader.ReadDouble();
                bsonReader.ReadEndArray();

                return new GeoJson2DGeographicCoordinates(longitude, latitude);
            }
        }
 public override object Deserialize(BsonReader bsonReader, Type nominalType,
     Type actualType, IBsonSerializationOptions options)
 {
     var bsonType = bsonReader.CurrentBsonType;
     switch (bsonType)
     {
         case BsonType.Null:
             bsonReader.ReadNull();
             return Guid.Empty;
         case BsonType.String:
             var readString = bsonReader.ReadString();
             return Guid.Parse(readString);
         default:
             var message = string.Format("Cannot deserialize BsonString from BsonType {0}.", bsonType);
             throw new BsonSerializationException(message);
     }
 }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>
        /// An object.
        /// </returns>
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                bsonReader.ReadStartArray();
                var x = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
                var y = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
                bsonReader.ReadEndArray();

                return new GeoJson2DCoordinates(x, y);
            }
        }
        public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options )
        {
            BsonType bsonType = bsonReader.CurrentBsonType;

            object result;

            if( bsonType == BsonType.Null )
            {
                bsonReader.ReadNull();
                result = null;
            }
            else
            {
                if( bsonType == BsonType.Document )
                {
                    var dictionary = new DynamicDictionary();

                    bsonReader.ReadStartDocument();

                    IDiscriminatorConvention valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention( typeof( object ) );

                    while( bsonReader.ReadBsonType() != BsonType.EndOfDocument )
                    {
                        string key = bsonReader.ReadName();
                        Type valueType = valueDiscriminatorConvention.GetActualType( bsonReader, typeof( object ) );
                        IBsonSerializer valueSerializer = BsonSerializer.LookupSerializer( valueType );
                        object value = valueSerializer.Deserialize( bsonReader, typeof( object ), valueType, null );

                        if( key != "_t" )
                        {
                            dictionary.Add( key.Replace( '\x03', '.' ), value );
                        }
                    }
                    bsonReader.ReadEndDocument();
                    result = dictionary;
                }
                else
                {
                    string message = string.Format( "Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType );
                    throw new BsonException( message );
                }
            }

            return result;
        }
        // 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:
                    BsonBinarySubType subType;
                    bsonReader.ReadBinaryData(out bytes, out subType);
                    if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
                    {
                        message = string.Format("Invalid Binary sub type {0}.", subType);
                        throw new FileFormatException(message);
                    }
                    return 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);
            }
        }