/// <summary>
        /// Creates a child context and calls the Deserialize method of the serializer with it.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="configurator">The configurator.</param>
        /// <returns>
        /// The deserialized value.
        /// </returns>
        public object DeserializeWithChildContext(
            IBsonSerializer serializer,
            Action <Builder> configurator = null)
        {
            var childContext = CreateChild(serializer.ValueType, configurator);

            return(serializer.Deserialize(childContext));
        }
        // protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonPolygonCoordinates <TCoordinates> DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;
            var holes      = new List <GeoJsonLinearRingCoordinates <TCoordinates> >();

            bsonReader.ReadStartArray();
            var exterior = _linearRingCoordinatesSerializer.Deserialize(context);

            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var hole = _linearRingCoordinatesSerializer.Deserialize(context);
                holes.Add(hole);
            }
            bsonReader.ReadEndArray();

            return(new GeoJsonPolygonCoordinates <TCoordinates>(exterior, holes));
        }
Example #3
0
        // private methods
        private TDictionary DeserializeArrayRepresentation(BsonDeserializationContext context)
        {
            var dictionary = CreateInstance();

            var bsonReader = context.Reader;

            bsonReader.ReadStartArray();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                object key;
                object value;

                var bsonType = bsonReader.GetCurrentBsonType();
                switch (bsonType)
                {
                case BsonType.Array:
                    bsonReader.ReadStartArray();
                    key   = _keySerializer.Deserialize(context);
                    value = _valueSerializer.Deserialize(context);
                    bsonReader.ReadEndArray();
                    break;

                case BsonType.Document:
                    key   = null;
                    value = null;
                    _helper.DeserializeMembers(context, (elementName, flag) =>
                    {
                        switch (flag)
                        {
                        case Flags.Key: key = _keySerializer.Deserialize(context); break;

                        case Flags.Value: value = _valueSerializer.Deserialize(context); break;
                        }
                    });
                    break;

                default:
                    throw CreateCannotDeserializeFromBsonTypeException(bsonType);
                }

                dictionary.Add(key, value);
            }
            bsonReader.ReadEndArray();

            return(dictionary);
        }
        // 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));
            }
        }
Example #5
0
 private T DeserializeDocument <T>(BsonDocument document, IBsonSerializer <T> serializer)
 {
     using (var reader = new BsonDocumentReader(document))
     {
         var deserializationContext = BsonDeserializationContext.CreateRoot(reader);
         return(serializer.Deserialize(deserializationContext));
     }
 }
        /// <summary>
        /// Creates a child context and calls the Deserialize method of the serializer with it.
        /// </summary>
        /// <typeparam name="TNominalType">The nominal type.</typeparam>
        /// <param name="serializer">The serializer.</param>
        /// <param name="configurator">The configurator.</param>
        /// <returns>
        /// The deserialized value.
        /// </returns>
        public TNominalType DeserializeWithChildContext <TNominalType>(
            IBsonSerializer <TNominalType> serializer,
            Action <Builder> configurator = null)
        {
            var childContext = CreateChild <TNominalType>(configurator);

            return(serializer.Deserialize(childContext));
        }
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <typeparam name="TValue">The type that this serializer knows how to serialize.</typeparam>
        /// <param name="serializer">The serializer.</param>
        /// <param name="context">The deserialization context.</param>
        /// <returns>A deserialized value.</returns>
        public static TValue Deserialize <TValue>(this IBsonSerializer <TValue> serializer, BsonDeserializationContext context)
        {
            var args = new BsonDeserializationArgs {
                NominalType = serializer.ValueType
            };

            return(serializer.Deserialize(context, args));
        }
Example #8
0
 private TDocument DeserializeDocument(RawBsonDocument rawDocument)
 {
     using (var stream = new ByteBufferStream(rawDocument.Slice, ownsBuffer: false))
         using (var reader = new BsonBinaryReader(stream))
         {
             var context = BsonDeserializationContext.CreateRoot(reader);
             return(_documentSerializer.Deserialize(context));
         }
 }
        // 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  = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
                var northing = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
                var altitude = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null);
                bsonReader.ReadEndArray();

                return(new GeoJson3DProjectedCoordinates(easting, northing, altitude));
            }
        }
        // public methods
        public List <IEnumerator <TDocument> > Execute(MongoConnection connection)
        {
            var command = new CommandDocument
            {
                { "parallelCollectionScan", _collectionName },
                { "numCursors", _numberOfCursors }
            };
            var options = new BsonDocument();
            var commandResultSerializer = BsonSerializer.LookupSerializer(typeof(CommandResult));

            var operation = new CommandOperation <CommandResult>(
                _databaseName,
                _readerSettings,
                _writerSettings,
                command,
                QueryFlags.None,
                options,
                _readPreference,
                null, // serializationOptions
                commandResultSerializer);

            var result   = operation.Execute(connection);
            var response = result.Response;

            var connectionProvider = new ServerInstanceConnectionProvider(result.ServerInstance);
            var collectionFullName = _databaseName + "." + _collectionName;

            var enumerators = new List <IEnumerator <TDocument> >();

            foreach (BsonDocument cursorsArrayItem in response["cursors"].AsBsonArray)
            {
                var cursor = cursorsArrayItem["cursor"].AsBsonDocument;

                var firstBatch = cursor["firstBatch"].AsBsonArray.Select(v =>
                {
                    using (var reader = BsonReader.Create(v.AsBsonDocument))
                    {
                        return((TDocument)_serializer.Deserialize(reader, typeof(TDocument), _serializationOptions));
                    }
                });
                var cursorId = cursor["id"].ToInt64();

                var enumerator = new CursorEnumerator <TDocument>(
                    connectionProvider,
                    collectionFullName,
                    firstBatch,
                    cursorId,
                    _batchSize ?? 0,
                    0, // limit
                    _readerSettings,
                    _serializer,
                    _serializationOptions);
                enumerators.Add(enumerator);
            }

            return(enumerators);
        }
        // public methods
        /// <summary>
        /// Deserializes the value.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        protected override Tuple <T1> DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            context.Reader.ReadStartArray();
            var item1 = _item1Serializer.Deserialize(context);

            context.Reader.ReadEndArray();

            return(new Tuple <T1>(item1));
        }
Example #12
0
        // internal methods
        internal void ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions)
        {
            if (serializationOptions == null && typeof(TDocument) == typeof(BsonDocument))
            {
                serializationOptions = DocumentSerializationOptions.AllowDuplicateNamesInstance;
            }

            var messageStartPosition = buffer.Position;

            ReadMessageHeaderFrom(buffer);
            _responseFlags  = (ResponseFlags)buffer.ReadInt32();
            _cursorId       = buffer.ReadInt64();
            _startingFrom   = buffer.ReadInt32();
            _numberReturned = buffer.ReadInt32();

            if ((_responseFlags & ResponseFlags.CursorNotFound) != 0)
            {
                throw new MongoQueryException("Cursor not found.");
            }
            if ((_responseFlags & ResponseFlags.QueryFailure) != 0)
            {
                BsonDocument document;
                using (BsonReader bsonReader = new BsonBinaryReader(buffer, false, _readerSettings))
                {
                    document = (BsonDocument)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), null);
                }
                var err     = document.GetValue("$err", "Unknown error.");
                var message = string.Format("QueryFailure flag was {0} (response was {1}).", err, document.ToJson());
                throw new MongoQueryException(message, document);
            }

            _documents = new List <TDocument>(_numberReturned);
            for (int i = 0; i < _numberReturned; i++)
            {
                BsonBuffer sliceBuffer;
                if (buffer.ByteBuffer is MultiChunkBuffer)
                {
                    // we can use slightly faster SingleChunkBuffers for all documents that don't span chunk boundaries
                    var position = buffer.Position;
                    var length   = buffer.ReadInt32();
                    var slice    = buffer.ByteBuffer.GetSlice(position, length);
                    buffer.Position = position + length;
                    sliceBuffer     = new BsonBuffer(slice, true);
                }
                else
                {
                    sliceBuffer = new BsonBuffer(buffer.ByteBuffer, false);
                }

                using (var bsonReader = new BsonBinaryReader(sliceBuffer, true, _readerSettings))
                {
                    var document = (TDocument)_serializer.Deserialize(bsonReader, typeof(TDocument), serializationOptions);
                    _documents.Add(document);
                }
            }
        }
Example #13
0
        private IEnumerable <(A, B)> EnumerateTuples(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var reader = context.Reader;

            var keyDeserializationArgs   = ArgumentHelper.GetSpecificDeserializationArgs(args);
            var valueDeserializationArgs = ArgumentHelper.GetSpecificDeserializationArgs(args, 1);

            reader.ReadStartArray();

            while (reader.ReadBsonType() != BsonType.EndOfDocument)
            {
                A key   = default;
                B value = default;

                var keySet   = false;
                var valueSet = false;

                reader.ReadStartDocument();
                while (reader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var elementName = reader.ReadName();
                    switch (elementName)
                    {
                    case Key:
                        keySet = true;
                        key    = _keySerializer.Deserialize(context, keyDeserializationArgs);
                        break;

                    case Value:
                        valueSet = true;
                        value    = _valueSerializer.Deserialize(context, valueDeserializationArgs);
                        break;

                    default:
                        throw new BsonSerializationException($"Unknown element name {elementName}");
                    }
                }

                reader.ReadEndDocument();

                if (!keySet)
                {
                    throw new BsonSerializationException("Missing key");
                }

                if (!valueSet)
                {
                    throw new BsonSerializationException("Missing value");
                }

                yield return(key, value);
            }

            reader.ReadEndArray();
        }
        // public methods
        public override TValue Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var reader = context.Reader;

            reader.ReadStartDocument();
            reader.ReadName(_fieldName);
            var value = _valueSerializer.Deserialize(context);

            reader.ReadEndDocument();
            return(value);
        }
        // methods
        /// <summary>
        /// Reads the message.
        /// </summary>
        /// <returns>A message.</returns>
        public ReplyMessage <TDocument> ReadMessage()
        {
            var binaryReader = CreateBinaryReader();
            var stream       = binaryReader.BsonStream;

            stream.ReadInt32(); // messageSize
            var requestId  = stream.ReadInt32();
            var responseTo = stream.ReadInt32();
            var opcode     = (Opcode)stream.ReadInt32();

            EnsureOpcodeIsValid(opcode);
            var flags                             = (ResponseFlags)stream.ReadInt32();
            var cursorId                          = stream.ReadInt64();
            var startingFrom                      = stream.ReadInt32();
            var numberReturned                    = stream.ReadInt32();
            List <TDocument> documents            = null;
            BsonDocument     queryFailureDocument = null;

            var awaitCapable   = (flags & ResponseFlags.AwaitCapable) == ResponseFlags.AwaitCapable;
            var cursorNotFound = (flags & ResponseFlags.CursorNotFound) == ResponseFlags.CursorNotFound;
            var queryFailure   = (flags & ResponseFlags.QueryFailure) == ResponseFlags.QueryFailure;

            if (queryFailure)
            {
                var context = BsonDeserializationContext.CreateRoot(binaryReader);
                queryFailureDocument = BsonDocumentSerializer.Instance.Deserialize(context);
            }
            else
            {
                documents = new List <TDocument>();
                for (var i = 0; i < numberReturned; i++)
                {
                    var allowDuplicateElementNames = typeof(TDocument) == typeof(BsonDocument);
                    var context = BsonDeserializationContext.CreateRoot(binaryReader, builder =>
                    {
                        builder.AllowDuplicateElementNames = allowDuplicateElementNames;
                    });
                    documents.Add(_serializer.Deserialize(context));
                }
            }

            return(new ReplyMessage <TDocument>(
                       awaitCapable,
                       cursorId,
                       cursorNotFound,
                       documents,
                       numberReturned,
                       queryFailure,
                       queryFailureDocument,
                       requestId,
                       responseTo,
                       _serializer,
                       startingFrom));
        }
Example #16
0
        // methods
        public InsertMessage <TDocument> ReadMessage()
        {
            if (_jsonReader == null)
            {
                throw new InvalidOperationException("No jsonReader was provided.");
            }

            var messageContext  = BsonDeserializationContext.CreateRoot <BsonDocument>(_jsonReader);
            var messageDocument = BsonDocumentSerializer.Instance.Deserialize(messageContext);

            var opcode = messageDocument["opcode"].AsString;

            if (opcode != "insert")
            {
                throw new FormatException("Opcode is not insert.");
            }

            var requestId       = messageDocument["requestId"].ToInt32();
            var databaseName    = messageDocument["database"].AsString;
            var collectionName  = messageDocument["collection"].AsString;
            var maxBatchCount   = messageDocument["maxBatchCount"].ToInt32();
            var maxMessageSize  = messageDocument["maxMessageSize"].ToInt32();
            var continueOnError = messageDocument["continueOnError"].ToBoolean();
            var documents       = messageDocument["documents"];

            if (documents.IsBsonNull)
            {
                throw new FormatException("InsertMessageJsonEncoder requires documents to not be null.");
            }

            var batch = new List <TDocument>();

            foreach (BsonDocument serializedDocument in documents.AsBsonArray)
            {
                using (var documentReader = new BsonDocumentReader(serializedDocument))
                {
                    var documentContext = BsonDeserializationContext.CreateRoot <TDocument>(documentReader);
                    var document        = _serializer.Deserialize(documentContext);
                    batch.Add(document);
                }
            }
            var documentSource = new BatchableSource <TDocument>(batch);

            return(new InsertMessage <TDocument>(
                       requestId,
                       databaseName,
                       collectionName,
                       _serializer,
                       documentSource,
                       maxBatchCount,
                       maxMessageSize,
                       continueOnError));
        }
        private TDocument DeserializeDocument(RawBsonDocument rawDocument)
        {
            var slice = rawDocument.Slice;
            var bytes = slice.AccessBackingBytes(0);

            using (var memoryStream = new MemoryStream(bytes.Array, bytes.Offset, bytes.Count))
                using (var reader = new BsonBinaryReader(memoryStream))
                {
                    var context = BsonDeserializationContext.CreateRoot(reader);
                    return(_documentSerializer.Deserialize(context));
                }
        }
Example #18
0
        public override Option <A> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            if (context.Reader.CurrentBsonType == BsonType.Null)
            {
                context.Reader.ReadNull();
                return(Option <A> .None);
            }

            var value = _itemSerializer.Deserialize(context, GetItemDeserializationArgs(args));

            return(Option <A> .Some(value));
        }
            public override TResult Deserialize(BsonDeserializationContext context)
            {
                var reader = context.Reader;

                if (reader.CurrentBsonType == BsonType.Null)
                {
                    reader.SkipValue();
                    return(default(TResult));
                }

                return(_resultSerializer.Deserialize(context));
            }
Example #20
0
        // methods
        public ReplyMessage <TDocument> ReadMessage()
        {
            if (_binaryReader == null)
            {
                throw new InvalidOperationException("No binaryReader was provided.");
            }

            var streamReader = _binaryReader.StreamReader;

            var messageSize                       = streamReader.ReadInt32();
            var requestId                         = streamReader.ReadInt32();
            var responseTo                        = streamReader.ReadInt32();
            var opcode                            = (Opcode)streamReader.ReadInt32();
            var flags                             = (ResponseFlags)streamReader.ReadInt32();
            var cursorId                          = streamReader.ReadInt64();
            var startingFrom                      = streamReader.ReadInt32();
            var numberReturned                    = streamReader.ReadInt32();
            List <TDocument> documents            = null;
            BsonDocument     queryFailureDocument = null;

            var awaitCapable   = flags.HasFlag(ResponseFlags.AwaitCapable);
            var cursorNotFound = flags.HasFlag(ResponseFlags.CursorNotFound);
            var queryFailure   = flags.HasFlag(ResponseFlags.QueryFailure);

            if (queryFailure)
            {
                var context = BsonDeserializationContext.CreateRoot <BsonDocument>(_binaryReader);
                queryFailureDocument = BsonDocumentSerializer.Instance.Deserialize(context);
            }
            else
            {
                documents = new List <TDocument>();
                for (var i = 0; i < numberReturned; i++)
                {
                    var context = BsonDeserializationContext.CreateRoot <TDocument>(_binaryReader);
                    documents.Add(_serializer.Deserialize(context));
                }
            }

            return(new ReplyMessage <TDocument>(
                       awaitCapable,
                       cursorId,
                       cursorNotFound,
                       documents,
                       numberReturned,
                       queryFailure,
                       queryFailureDocument,
                       requestId,
                       responseTo,
                       _serializer,
                       startingFrom));
        }
Example #21
0
        /// <inheritdoc/>
        public override Money Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var reader   = context.Reader;
            var bsonType = reader.GetCurrentBsonType();

            if (bsonType == BsonType.Null)
            {
                reader.ReadNull();
                return(Money.Zero);
            }

            if (bsonType != BsonType.Document)
            {
                throw new FormatException($"Cannot deserialize Money from BsonType {bsonType}.");
            }

            reader.ReadStartDocument();

            decimal?  amount   = null;
            ICurrency currency = null;

            while (reader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var name = reader.ReadName();

                if (name == _amountFieldName)
                {
                    amount = _amountSerializer.Deserialize(context, args);
                }
                else if (name == _currencyFieldName)
                {
                    currency = _currencySerializer.Deserialize(context, args);
                }
                else
                {
                    throw new BsonSerializationException($"Invalid element: '{name}'.");
                }
            }

            reader.ReadEndDocument();

            if (currency is null)
            {
                throw new BsonSerializationException($"Document does not contains field {_currencyFieldName}");
            }
            if (amount is null)
            {
                throw new BsonSerializationException($"Document does not contains field {_amountFieldName}");
            }

            return(new Money(amount.Value, currency));
        }
Example #22
0
        // private methods
        private IEnumerable <TValue> ReadValues(BsonReader bsonReader)
        {
            var values = new List <TValue>();

            bsonReader.ReadStartArray();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                values.Add((TValue)_valueSerializer.Deserialize(bsonReader, typeof(TValue), _valueSerializationOptions));
            }
            bsonReader.ReadEndArray();

            return(values);
        }
        /// <summary>
        /// Deserializes the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The deserialized value.</returns>
        public object DeserializeValue(BsonValue value)
        {
            var tempDocument = new BsonDocument("value", value);

            using (var reader = BsonReader.Create(tempDocument))
            {
                reader.ReadStartDocument();
                reader.ReadName("value");
                var deserializedValue = _serializer.Deserialize(reader, _nominalType, _serializationOptions);
                reader.ReadEndDocument();
                return(deserializedValue);
            }
        }
Example #24
0
 // private methods
 private TEnum Deserialize <TEnum>(IBsonSerializer <TEnum> serializer, byte[] bson)
 {
     using (var stream = new MemoryStream(bson))
         using (var reader = new BsonBinaryReader(stream))
         {
             var context = BsonDeserializationContext.CreateRoot(reader);
             reader.ReadStartDocument();
             reader.ReadName("x");
             var value = serializer.Deserialize(context);
             reader.ReadEndDocument();
             return(value);
         }
 }
Example #25
0
        private IEnumerable <B> EnumerateValues(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var reader = context.Reader;

            reader.ReadStartArray();
            var itemDeserializationArgs = GetItemDeserializationArgs(args);

            while (reader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var item = _itemSerializer.Deserialize(context, itemDeserializationArgs);
                yield return(item);
            }
            reader.ReadEndArray();
        }
Example #26
0
        /// <summary>
        /// Deserializes the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>A deserialized value.</returns>
        public object DeserializeValue(BsonValue value)
        {
            var tempDocument = new BsonDocument("value", value);

            using (var reader = new BsonDocumentReader(tempDocument))
            {
                var context = BsonDeserializationContext.CreateRoot(reader);
                reader.ReadStartDocument();
                reader.ReadName("value");
                var deserializedValue = _serializer.Deserialize(context);
                reader.ReadEndDocument();
                return(deserializedValue);
            }
        }
Example #27
0
        // private methods
        private IEnumerable <TValue> ReadValues(BsonDeserializationContext context)
        {
            var bsonReader = context.Reader;
            var values     = new List <TValue>();

            bsonReader.ReadStartArray();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                values.Add(_valueSerializer.Deserialize(context));
            }
            bsonReader.ReadEndArray();

            return(values);
        }
        private List <TDocument> ReadDocuments(BsonBinaryReader reader, long messageStartPosition, int messageSize)
        {
            var stream    = reader.BsonStream;
            var context   = BsonDeserializationContext.CreateRoot(reader);
            var documents = new List <TDocument>();

            while (stream.Position < messageStartPosition + messageSize)
            {
                var document = _serializer.Deserialize(context);
                documents.Add(document);
            }

            return(documents);
        }
Example #29
0
        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <param name="args">The deserialization args.</param>
        /// <returns>A deserialized value.</returns>
        /// <exception cref="System.FormatException"></exception>
        public override TInterface Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            var bsonReader = context.Reader;

            if (bsonReader.GetCurrentBsonType() == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(default(TInterface));
            }
            else
            {
                return(_implementationSerializer.Deserialize(context));
            }
        }
Example #30
0
        // 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
            {
                var holes = new List <GeoJsonLinearRingCoordinates <TCoordinates> >();

                bsonReader.ReadStartArray();
                var exterior = (GeoJsonLinearRingCoordinates <TCoordinates>)_linearRingSerializer.Deserialize(bsonReader, typeof(TCoordinates), null);
                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var hole = (GeoJsonLinearRingCoordinates <TCoordinates>)_linearRingSerializer.Deserialize(bsonReader, typeof(TCoordinates), null);
                    holes.Add(hole);
                }
                bsonReader.ReadEndArray();

                return(new GeoJsonPolygonCoordinates <TCoordinates>(exterior, holes));
            }
        }