MongoDB.Bson.Serialization.Serializers.StringSerializer.Deserialize C# (CSharp) Метод

Deserialize() публичный Метод

Deserializes an object from a BsonReader.
public Deserialize ( MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options ) : object
bsonReader MongoDB.Bson.IO.BsonReader The BsonReader.
nominalType System.Type The nominal type of the object.
actualType System.Type The actual type of the object.
options IBsonSerializationOptions The serialization options.
Результат object
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(string));
            var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);

            var bsonType = bsonReader.GetCurrentBsonType();
            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return null;
            }
            else
            {
                switch (bsonType)
                {
                    case BsonType.ObjectId:
                        if (representationSerializationOptions.Representation == BsonType.ObjectId)
                        {
                            return bsonReader.ReadObjectId().ToString();
                        }
                        else
                        {
                            goto default;
                        }
                    case BsonType.String:
                        return bsonReader.ReadString();
                    case BsonType.Symbol:
                        return bsonReader.ReadSymbol();
                    default:
                        var message = string.Format("Cannot deserialize string from BsonType {0}.", bsonType);
                        throw new Exception(message);
                }
            }
        }

Usage Example

        public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
        {
            var bsonType = bsonReader.GetCurrentBsonType();
              if (bsonType == BsonType.Null)
              {
            bsonReader.ReadNull();
            return null;
              }

              var serializer = new StringSerializer();
              var nvc = new NameValueCollection();

              bsonReader.ReadStartArray();
              while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
              {
            bsonReader.ReadStartArray();
            var key = (string) serializer.Deserialize(bsonReader, typeof (string), options);
            var val = (string) serializer.Deserialize(bsonReader, typeof (string), options);
            bsonReader.ReadEndArray();
            nvc.Add(key, val);
              }
              bsonReader.ReadEndArray();

              return nvc;
        }