Represents a serializer for BsonDocuments.
Наследование: MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer
        public void ResultSerializer_get_should_return_expected_result()
        {
            var resultSerializer = new BsonDocumentSerializer();
            var subject = new FindOpcodeOperation<BsonDocument>(_collectionNamespace, resultSerializer, _messageEncoderSettings);

            var result = subject.ResultSerializer;

            result.Should().Be(resultSerializer);
        }
Пример #2
0
        internal void Save(string saveAs, FileFormat format)
        {
            if (string.IsNullOrEmpty(saveAs))
            {
                saveAs = FilePath;
                format = FileFormat;
            }

            if (saveAs == null)
                throw new InvalidOperationException("File path should be provided either on opening or saving.");

            if (format == FileFormat.Auto)
                format = saveAs.EndsWith(".json", StringComparison.OrdinalIgnoreCase) ? FileFormat.Json : FileFormat.Bson;

            var tmp = File.Exists(saveAs) ? saveAs + ".tmp" : saveAs;

            var serializer = new BsonDocumentSerializer();
            var options = DocumentSerializationOptions.Defaults;

            if (format == FileFormat.Json)
            {
                using (var streamWriter = new StreamWriter(tmp))
                {
                    foreach (var document in Documents)
                    {
                        using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
                        using (var bsonWriter = BsonWriter.Create(stringWriter, Actor.DefaultJsonWriterSettings))
                        {
                            serializer.Serialize(bsonWriter, typeof(BsonDocument), document, options);
                            streamWriter.WriteLine(stringWriter.ToString());
                        }
                    }
                }
            }
            else
            {
                using (var fileStream = File.Open(tmp, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var bsonWriter = BsonWriter.Create(fileStream))
                {
                    foreach (var document in Documents)
                        serializer.Serialize(bsonWriter, typeof(BsonDocument), document, options);
                }
            }

            if (!object.ReferenceEquals(tmp, saveAs))
                File.Replace(tmp, saveAs, null);
        }