// protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonBoundingBox <TCoordinates> DeserializeValue(BsonDeserializationContext context)
        {
            var bsonReader = context.Reader;

            var flattenedArray = BsonArraySerializer.Instance.Deserialize(context.CreateChild(typeof(BsonArray)));

            if ((flattenedArray.Count % 2) != 0)
            {
                throw new FormatException("Bounding box array does not have an even number of values.");
            }
            var half = flattenedArray.Count / 2;

            // create a dummy document with a min and a max and then deserialize the min and max coordinates from there
            var document = new BsonDocument
            {
                { "min", new BsonArray(flattenedArray.Take(half)) },
                { "max", new BsonArray(flattenedArray.Skip(half)) }
            };

            using (var documentReader = new BsonDocumentReader(document))
            {
                var documentContext = BsonDeserializationContext.CreateRoot(documentReader, typeof(BsonDocument));
                documentReader.ReadStartDocument();
                documentReader.ReadName("min");
                var min = documentContext.DeserializeWithChildContext(_coordinatesSerializer);
                documentReader.ReadName("max");
                var max = documentContext.DeserializeWithChildContext(_coordinatesSerializer);
                documentReader.ReadEndDocument();

                return(new GeoJsonBoundingBox <TCoordinates>(min, max));
            }
        }
        // protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonMultiLineStringCoordinates <TCoordinates> DeserializeValue(BsonDeserializationContext context)
        {
            var bsonReader  = context.Reader;
            var lineStrings = new List <GeoJsonLineStringCoordinates <TCoordinates> >();

            bsonReader.ReadStartArray();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var lineString = _lineStringCoordinatesSerializer.Deserialize(context.CreateChild(_lineStringCoordinatesSerializer.ValueType));
                lineStrings.Add(lineString);
            }
            bsonReader.ReadEndArray();

            return(new GeoJsonMultiLineStringCoordinates <TCoordinates>(lineStrings));
        }
Esempio n. 3
0
        // protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonMultiPointCoordinates <TCoordinates> DeserializeValue(BsonDeserializationContext context)
        {
            var bsonReader = context.Reader;
            var positions  = new List <TCoordinates>();

            bsonReader.ReadStartArray();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var position = _coordinatesSerializer.Deserialize(context.CreateChild(_coordinatesSerializer.ValueType));
                positions.Add(position);
            }
            bsonReader.ReadEndArray();

            return(new GeoJsonMultiPointCoordinates <TCoordinates>(positions));
        }
Esempio n. 4
0
        // public methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>An object.</returns>
        public override TValue Deserialize(BsonDeserializationContext context)
        {
            var bsonReader  = context.Reader;
            var nominalType = context.NominalType;
            var actualType  = _discriminatorConvention.GetActualType(bsonReader, nominalType);
            var serializer  = BsonSerializer.LookupSerializer(actualType);

            TValue value = default(TValue);

            _helper.DeserializeMembers(context, (elementName, flag) =>
            {
                switch (flag)
                {
                case Flags.Discriminator: bsonReader.SkipValue(); break;

                case Flags.Value: value = (TValue)serializer.Deserialize(context.CreateChild(actualType)); break;
                }
            });

            return(value);
        }
Esempio n. 5
0
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        public override DistinctCommandResult <TValue> Deserialize(BsonDeserializationContext context)
        {
            var bsonReader = context.Reader;
            var response   = new BsonDocument();
            IEnumerable <TValue> values = null;

            bsonReader.ReadStartDocument();
            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                var name = bsonReader.ReadName();
                if (name == "values")
                {
                    values = ReadValues(context);
                }
                else
                {
                    var value = BsonValueSerializer.Instance.Deserialize(context.CreateChild(typeof(BsonValue)));
                    response.Add(name, value);
                }
            }
            bsonReader.ReadEndDocument();

            return(new DistinctCommandResult <TValue>(response, values));
        }
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        public override TCommandResult Deserialize(BsonDeserializationContext context)
        {
            var response = BsonDocumentSerializer.Instance.Deserialize(context.CreateChild(typeof(BsonDocument)));

            return((TCommandResult)Activator.CreateInstance(typeof(TCommandResult), new object[] { response }));
        }