/// <summary>
        /// Serializes an object to a BsonWriter.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <param name="value">The object.</param>
        /// <param name="options">The serialization options.</param>
        public override void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var lineStringCoordinates = (GeoJsonLineStringCoordinates <TCoordinates>)value;

                bsonWriter.WriteStartArray();
                foreach (var position in lineStringCoordinates.Positions)
                {
                    _coordinatesSerializer.Serialize(bsonWriter, typeof(TCoordinates), position, null);
                }
                bsonWriter.WriteEndArray();
            }
        }
Beispiel #2
0
            public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IEnumerable <TItem> value)
            {
                var bsonWriter = context.Writer;

                if (value == null)
                {
                    bsonWriter.WriteNull();
                }
                else
                {
                    bsonWriter.WriteStartArray();
                    foreach (var item in value)
                    {
                        _itemSerializer.Serialize(context, item);
                    }
                    bsonWriter.WriteEndArray();
                }
            }
Beispiel #3
0
    void IBsonSerializer.Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
    {
        MyClassA item = (MyClassA)value;

        if (item.myClassD != null)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                item.myClassD.Save(item.myClassD, stream);
                mesure.myClassDBin = stream.ToArray();
            }
        }
        else
        {
            item.myClassDBin = null;
        }
        _classMapSerializer.Serialize(bsonWriter, nominalType, item, options);
        item.myClassDBin = null;
    }
        // public methods
        /// <inheritdoc />
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource <TItem> value)
        {
            Ensure.IsNotNull(value, nameof(value));
            var writer = context.Writer;

            writer.PushElementNameValidator(_itemElementNameValidator);
            try
            {
                for (var i = 0; i < _count; i++)
                {
                    var item = value.Items[value.Offset + i];
                    _itemSerializer.Serialize(context, item);
                }
            }
            finally
            {
                writer.PopElementNameValidator();
            }
        }
Beispiel #5
0
        public static BsonArray SerializeValues(IBsonSerializer itemSerializer, IEnumerable values)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("_v");
                writer.WriteStartArray();
                var context = BsonSerializationContext.CreateRoot(writer);
                foreach (var value in values)
                {
                    itemSerializer.Serialize(context, value);
                }
                writer.WriteEndArray();
                writer.WriteEndDocument();
            }
            return(document["_v"].AsBsonArray);
        }
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The object.</param>
        protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TItem[,] value)
        {
            var bsonWriter = context.Writer;

            var length1 = value.GetLength(0);
            var length2 = value.GetLength(1);

            bsonWriter.WriteStartArray();
            for (int i = 0; i < length1; i++)
            {
                bsonWriter.WriteStartArray();
                for (int j = 0; j < length2; j++)
                {
                    _itemSerializer.Serialize(context, value[i, j]);
                }
                bsonWriter.WriteEndArray();
            }
            bsonWriter.WriteEndArray();
        }
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The object.</param>
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value)
        {
            var bsonWriter = context.Writer;

            var metaObject     = value.GetMetaObject(Expression.Constant(value));
            var memberNames    = metaObject.GetDynamicMemberNames();
            var dynamicContext = context.With(ConfigureSerializationContext);

            bsonWriter.WriteStartDocument();
            foreach (var memberName in memberNames)
            {
                object memberValue;
                if (TryGetValueForMember(value, memberName, out memberValue))
                {
                    bsonWriter.WriteName(memberName);
                    _objectSerializer.Serialize(dynamicContext, memberValue);
                }
            }
            bsonWriter.WriteEndDocument();
        }
        protected string Serialize <T>(T value)
        {
            IBsonSerializer <T> serializer = BsonSerializer.LookupSerializer <T>();

            string result;

            using (var textWriter = new StringWriter())
                using (var writer = new JsonWriter(textWriter))
                {
                    var context = BsonSerializationContext.CreateRoot(writer);
                    var args    = new BsonSerializationArgs {
                        NominalType = typeof(T)
                    };

                    serializer.Serialize(context, args, value);
                    result = textWriter.ToString();
                }

            return(result);
        }
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Map <A, B> value)
        {
            var writer = context.Writer;

            var keySerializationArgs   = ArgumentHelper.GetSpecificSerializationArgs(args);
            var valueSerializationArgs = ArgumentHelper.GetSpecificSerializationArgs(args, 1);

            writer.WriteStartArray();
            foreach (var(k, v) in value)
            {
                writer.WriteStartDocument();
                writer.WriteName(Key);
                _keySerializer.Serialize(context, keySerializationArgs, k);
                writer.WriteName(Value);
                _valueSerializer.Serialize(context, valueSerializationArgs, v);
                writer.WriteEndDocument();
            }

            writer.WriteEndArray();
        }
Beispiel #10
0
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="args">The serialization args.</param>
        /// <param name="value">The document.</param>
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TInterface value)
        {
            var bsonWriter = context.Writer;

            if (value == null)
            {
                bsonWriter.WriteNull();
            }
            else
            {
                var actualType = value.GetType();
                if (actualType == typeof(TImplementation))
                {
                    _implementationSerializer.Serialize(context, (TImplementation)value);
                }
                else
                {
                    var serializer = BsonSerializer.LookupSerializer(actualType);
                    serializer.Serialize(context, value);
                }
            }
        }
Beispiel #11
0
        protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, ChunkColumnCompactStorage value)
        {
            var writer = context.Writer;

            writer.WriteStartDocument();

            if (value.Biomes != null)
            {
                writer.WriteName(nameof(ChunkColumnCompactStorage.Biomes));
                writer.WriteBytes(value.Biomes);
            }

            writer.WriteName(nameof(ChunkColumnCompactStorage.Sections));
            writer.WriteStartArray();
            foreach (var section in value.Sections)
            {
                _serializer.Serialize(context, section);
            }
            writer.WriteEndArray();

            writer.WriteEndDocument();
        }
Beispiel #12
0
        private void WriteDocuments(BsonBinaryWriter writer, long messageStartPosition, InsertMessage <TDocument> message)
        {
            var stream  = writer.BsonStream;
            var context = BsonSerializationContext.CreateRoot(writer);

            var documentSource = message.DocumentSource;
            var batchCount     = Math.Min(documentSource.Count, message.MaxBatchCount);

            if (batchCount < documentSource.Count && !documentSource.CanBeSplit)
            {
                throw new BsonSerializationException("Batch is too large.");
            }

            for (var i = 0; i < batchCount; i++)
            {
                var document = documentSource.Items[documentSource.Offset + i];
                var documentStartPosition = stream.Position;

                _serializer.Serialize(context, document);

                var messageSize = stream.Position - messageStartPosition;
                if (messageSize > message.MaxMessageSize)
                {
                    if (i > 0 && documentSource.CanBeSplit)
                    {
                        stream.Position = documentStartPosition;
                        stream.SetLength(documentStartPosition);
                        documentSource.SetProcessedCount(i);
                        return;
                    }
                    else
                    {
                        throw new BsonSerializationException("Batch is too large.");
                    }
                }
            }
            documentSource.SetProcessedCount(batchCount);
        }
Beispiel #13
0
        protected byte[] Serialize <T>(IBsonSerializer <T> serializer, T value, bool mustBeNested = false, GuidRepresentation guidRepresentation = GuidRepresentation.Unspecified)
        {
            using (var memoryStream = new MemoryStream())
                using (var writer = new BsonBinaryWriter(memoryStream, new BsonBinaryWriterSettings {
                    GuidRepresentation = guidRepresentation
                }))
                {
                    if (mustBeNested)
                    {
                        writer.WriteStartDocument();
                        writer.WriteName("x");
                    }

                    var context = BsonSerializationContext.CreateRoot(writer);
                    serializer.Serialize(context, value);

                    if (mustBeNested)
                    {
                        writer.WriteEndDocument();
                    }

                    return(memoryStream.ToArray());
                }
        }
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, ISet <T> value)
 {
     _serializer.Serialize(context, args, value);
 }
 /// <summary>
 /// Serializes the value.
 /// </summary>
 /// <param name="context">The serialization context.</param>
 /// <param name="args">The serialization args.</param>
 /// <param name="value">The value.</param>
 protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, Tuple <T1> value)
 {
     context.Writer.WriteStartArray();
     _item1Serializer.Serialize(context, value.Item1);
     context.Writer.WriteEndArray();
 }
Beispiel #16
0
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDerivedDocument value)
 {
     args.NominalType = typeof(TRootDocument);
     _derivedDocumentSerializer.Serialize(context, args, value);
 }
 private void SerializeGeometry(BsonSerializationContext context, GeoJsonGeometry <TCoordinates> geometry)
 {
     context.Writer.WriteName("geometry");
     _geometrySerializer.Serialize(context, geometry);
 }
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
 {
     _sortBySerializer.Serialize(context, args, Coerce(value));
 }
Beispiel #19
0
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value)
 {
     _serializer.Serialize(context, args, (TTo)Enum.ToObject(typeof(TTo), (object)value));
 }
Beispiel #20
0
        public void Serialize(BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
        {
            var hashSet = (Collection.HashSet <T>)value;

            _enumerableSerializer.Serialize(bsonWriter, typeof(ISet <T>), new HashSet <T>(hashSet), options);
        }
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TEnumUnderlyingType value)
        {
            var enumValue = (TEnum)(object)value;

            _enumSerializer.Serialize(context, enumValue);
        }
Beispiel #22
0
 private void SerializeCoordinates(BsonSerializationContext context, TCoordinates coordinates)
 {
     context.Writer.WriteName("coordinates");
     _coordinatesSerializer.Serialize(context, coordinates);
 }
 public override void Serialize(BsonSerializationContext context, GeoNearResult <TDocument> value)
 {
     _serializer.Serialize(context, value);
 }
 private void SerializeCoordinates(BsonSerializationContext context, GeoJsonMultiPointCoordinates <TCoordinates> coordinates)
 {
     context.Writer.WriteName("coordinates");
     _coordinatesSerializer.Serialize(context, coordinates);
 }
Beispiel #25
0
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value)
 {
     _serializer.Serialize(context, args, (TTo)(object)value);
 }
Beispiel #26
0
 public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, GeoHaystackSearchResult <TDocument> value)
 {
     _serializer.Serialize(context, value);
 }
Beispiel #27
0
        // public methods
        /// <inheritdoc />
        public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource <TItem> value)
        {
            Ensure.IsNotNull(value, nameof(value));

            var writer = context.Writer;

            while (writer is WrappingBsonWriter)
            {
                writer = ((WrappingBsonWriter)writer).Wrapped;
            }

            var binaryWriter  = writer as BsonBinaryWriter;
            var startPosition = binaryWriter?.Position;

            writer.PushSettings(s => { var bs = s as BsonBinaryWriterSettings; if (bs != null)
                                       {
                                           bs.MaxDocumentSize = _maxItemSize;
                                       }
                                });
            writer.PushElementNameValidator(_itemElementNameValidator);
            try
            {
                var batchCount = Math.Min(value.Count, _maxBatchCount);
                if (batchCount != value.Count && !value.CanBeSplit)
                {
                    throw new ArgumentException("Batch is too large.");
                }

                for (var i = 0; i < batchCount; i++)
                {
                    var itemPosition = binaryWriter?.Position;

                    var item = value.Items[value.Offset + i];
                    _itemSerializer.Serialize(context, args, item);

                    // always process at least one item
                    if (i > 0)
                    {
                        var batchSize = binaryWriter?.Position - startPosition;
                        if (batchSize > _maxBatchSize)
                        {
                            if (value.CanBeSplit)
                            {
                                binaryWriter.BaseStream.Position = itemPosition.Value; // remove the last item
                                binaryWriter.BaseStream.SetLength(itemPosition.Value);
                                value.SetProcessedCount(i);
                                return;
                            }
                            else
                            {
                                throw new ArgumentException("Batch is too large.");
                            }
                        }
                    }
                }
                value.SetProcessedCount(batchCount);
            }
            finally
            {
                writer.PopElementNameValidator();
                writer.PopSettings();
            }
        }
 public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
 {
     enumerableSerializer.Serialize(context, args, value);
 }