• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# IO.BsonBinaryWriter类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中MongoDB.Bson.IO.BsonBinaryWriter的典型用法代码示例。如果您正苦于以下问题:C# BsonBinaryWriter类的具体用法?C# BsonBinaryWriter怎么用?C# BsonBinaryWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



BsonBinaryWriter类属于MongoDB.Bson.IO命名空间,在下文中一共展示了BsonBinaryWriter类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: TestIsAtEndOfFileWithTwoDocuments

        public void TestIsAtEndOfFileWithTwoDocuments()
        {
            var expected = new BsonDocument("x", 1);

            byte[] bson;
            using (var stream = new MemoryStream())
            using (var writer = new BsonBinaryWriter(stream))
            {
                BsonSerializer.Serialize(writer, expected);
                BsonSerializer.Serialize(writer, expected);
                bson = stream.ToArray();
            }

            using (var stream = new MemoryStream(bson))
            using (var reader = new BsonBinaryReader(stream))
            {
                var count = 0;
                while (!reader.IsAtEndOfFile())
                {
                    var document = BsonSerializer.Deserialize<BsonDocument>(reader);
                    Assert.AreEqual(expected, document);
                    count++;
                }
                Assert.AreEqual(2, count);
            }
        }
开发者ID:Bogdan0x400,项目名称:mongo-csharp-driver,代码行数:26,代码来源:BsonBinaryReaderTests.cs


示例2: TestNoCircularReference

        public void TestNoCircularReference()
        {
            var c2 = new C { X = 2 };
            var c1 = new C { X = 1, NestedDocument = c2 };

            var json = c1.ToJson();
            var expected = "{ 'X' : 1, 'NestedDocument' : { 'X' : 2, 'NestedDocument' : null, 'BsonArray' : { '_csharpnull' : true } }, 'BsonArray' : { '_csharpnull' : true } }".Replace("'", "\"");
            Assert.AreEqual(expected, json);

            var memoryStream = new MemoryStream();
            using (var writer = new BsonBinaryWriter(memoryStream))
            {
                BsonSerializer.Serialize(writer, c1);
                Assert.AreEqual(0, writer.SerializationDepth);
            }

            var document = new BsonDocument();
            using (var writer = new BsonDocumentWriter(document))
            {
                BsonSerializer.Serialize(writer, c1);
                Assert.AreEqual(0, writer.SerializationDepth);
            }

            var stringWriter = new StringWriter();
            using (var writer = new JsonWriter(stringWriter))
            {
                BsonSerializer.Serialize(writer, c1);
                Assert.AreEqual(0, writer.SerializationDepth);
            }
        }
开发者ID:Bogdan0x400,项目名称:mongo-csharp-driver,代码行数:30,代码来源:CircularReferencesTests.cs


示例3: TestWriteNameThrowsWhenValueIsNull

 public void TestWriteNameThrowsWhenValueIsNull()
 {
     using (var stream = new MemoryStream())
     using (var bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
     {
         Assert.Throws<ArgumentNullException>(() => { bsonWriter.WriteName(null); });
     }
 }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:8,代码来源:BsonWriterTests.cs


示例4: TestWriteNameThrowsWhenValueContainsNulls

 public void TestWriteNameThrowsWhenValueContainsNulls()
 {
     using (var stream = new MemoryStream())
     using (var bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
     {
         Assert.Throws<BsonSerializationException>(() => { bsonWriter.WriteName("a\0b"); });
     }
 }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:8,代码来源:BsonWriterTests.cs


示例5: BsonBinaryReader_should_support_reading_more_than_2GB

        public void BsonBinaryReader_should_support_reading_more_than_2GB()
        {
            RequireEnvironmentVariable.IsDefined("EXPLICIT");

            var binaryData = new BsonBinaryData(new byte[1024 * 1024]);

            var tempFileName = Path.GetTempFileName();
            try
            {
                using (var stream = new FileStream(tempFileName, FileMode.Open))
                {
                    using (var binaryWriter = new BsonBinaryWriter(stream))
                    {
                        while (stream.Position < (long)int.MaxValue * 4)
                        {
                            binaryWriter.WriteStartDocument();
                            binaryWriter.WriteName("x");
                            binaryWriter.WriteBinaryData(binaryData);
                            binaryWriter.WriteEndDocument();
                        }
                    }

                    var endOfFilePosition = stream.Position;
                    stream.Position = 0;

                    using (var binaryReader = new BsonBinaryReader(stream))
                    {
                        while (!binaryReader.IsAtEndOfFile())
                        {
                            binaryReader.ReadStartDocument();
                            var bookmark = binaryReader.GetBookmark();

                            binaryReader.ReadName("x");
                            binaryReader.ReturnToBookmark(bookmark);

                            binaryReader.ReadName("x");
                            var readBinaryData = binaryReader.ReadBinaryData();
                            Assert.Equal(binaryData.Bytes.Length, readBinaryData.Bytes.Length);

                            binaryReader.ReadEndDocument();
                        }
                    }

                    Assert.Equal(endOfFilePosition, stream.Position);
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempFileName);
                }
                catch
                {
                    // ignore exceptions
                }
            }
        }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:58,代码来源:BsonBinaryReaderTests.cs


示例6: Constructor_should_not_throw_if_only_binaryWriter_is_provided

 public void Constructor_should_not_throw_if_only_binaryWriter_is_provided()
 {
     using (var stream = new MemoryStream())
     using (var binaryWriter = new BsonBinaryWriter(stream))
     {
         Action action = () => new GetMoreMessageBinaryEncoder(null, binaryWriter);
         action.ShouldNotThrow();
     }
 }
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:9,代码来源:GetMoreMessageBinaryEncoderTests.cs


示例7: Constructor_with_two_parameters_should_not_throw_if_only_binaryWriter_is_provided

 public void Constructor_with_two_parameters_should_not_throw_if_only_binaryWriter_is_provided()
 {
     using (var stream = new MemoryStream())
     using (var binaryWriter = new BsonBinaryWriter(stream))
     {
         Action action = () => new BinaryMessageEncoderFactory(null, binaryWriter);
         action.ShouldNotThrow();
     }
 }
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:9,代码来源:BinaryMessageEncoderFactoryTests.cs


示例8: AddDocument

 // internal methods
 internal void AddDocument(BsonBuffer buffer, Type nominalType, object document)
 {
     _lastDocumentStartPosition = buffer.Position;
     using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
     {
         bsonWriter.CheckElementNames = _checkElementNames;
         BsonSerializer.Serialize(bsonWriter, nominalType, document, DocumentSerializationOptions.SerializeIdFirstInstance);
     }
     BackpatchMessageLength(buffer);
 }
开发者ID:pwelter34,项目名称:mongo-csharp-driver,代码行数:11,代码来源:MongoInsertMessage.cs


示例9: Constructor_should_not_throw_if_binaryReader_and_binaryWriter_are_both_provided

 public void Constructor_should_not_throw_if_binaryReader_and_binaryWriter_are_both_provided()
 {
     using (var stream = new MemoryStream())
     using (var binaryReader = new BsonBinaryReader(stream))
     using (var binaryWriter = new BsonBinaryWriter(stream))
     {
         Action action = () => new DeleteMessageBinaryEncoder(binaryReader, binaryWriter);
         action.ShouldNotThrow();
     }
 }
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:10,代码来源:DeleteMessageBinaryEncoderTests.cs


示例10: TestInvalidKeys

        public void TestInvalidKeys(string key)
        {
            var c = new C { Id = 1, D = new Hashtable { { key, 2 } }, G = new Dictionary<object, int> { { key, 3 } } };

            using (var stream = new MemoryStream())
            using (var bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
            {
                bsonWriter.PushElementNameValidator(CollectionElementNameValidator.Instance);
                Assert.Throws<BsonSerializationException>(() => BsonSerializer.Serialize(bsonWriter, c));
            }
        }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:11,代码来源:CSharp624Tests.cs


示例11: SerializeRequest

 // protected methods
 protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
 {
     var deleteRequest = (DeleteRequest)request;
     bsonWriter.PushMaxDocumentSize(MaxDocumentSize);
     bsonWriter.WriteStartDocument();
     bsonWriter.WriteName("q");
     BsonSerializer.Serialize(bsonWriter, deleteRequest.Query ?? new QueryDocument());
     bsonWriter.WriteInt32("limit", deleteRequest.Limit);
     bsonWriter.WriteEndDocument();
     bsonWriter.PopMaxDocumentSize();
 }
开发者ID:Bogdan0x400,项目名称:mongo-csharp-driver,代码行数:12,代码来源:BulkDeleteOperation.cs


示例12: Test1ChunkPlus1

 public void Test1ChunkPlus1()
 {
     var stream = new MemoryStream();
     using (var bsonWriter = new BsonBinaryWriter(stream))
     {
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteBytes("Data", new byte[16 * 1024 - 15]);
         bsonWriter.WriteEndDocument();
         bsonWriter.Close();
     }
 }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:11,代码来源:CSharp116Tests.cs


示例13: TestFlushAndClose

 public void TestFlushAndClose()
 {
     var stream = new MemoryStream();
     using (var bsonWriter = new BsonBinaryWriter(stream))
     {
         bsonWriter.WriteStartDocument();
         bsonWriter.WriteEndDocument();
         bsonWriter.Flush();
         bsonWriter.Close();
     }
 }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:11,代码来源:CSharp116Tests.cs


示例14: FromBsonDocument

 public static RawBsonDocument FromBsonDocument(BsonDocument document)
 {
     using (var memoryStream = new MemoryStream())
     {
         using (var bsonWriter = new BsonBinaryWriter(memoryStream, BsonBinaryWriterSettings.Defaults))
         {
             var context = BsonSerializationContext.CreateRoot(bsonWriter);
             BsonDocumentSerializer.Instance.Serialize(context, document);
         }
         return new RawBsonDocument(memoryStream.ToArray());
     }
 }
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:12,代码来源:RawBsonDocumentHelper.cs


示例15: WriteBodyTo

 // internal methods
 internal override void WriteBodyTo(BsonBuffer buffer)
 {
     using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
     {
         bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
         if (_query == null)
         {
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteEndDocument();
         }
         else
         {
             BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, DocumentSerializationOptions.SerializeIdFirstInstance);
         }
         bsonWriter.PopMaxDocumentSize();
     }
 }
开发者ID:Khosrow-Azizi,项目名称:MasterExperimentV2,代码行数:18,代码来源:MongoDeleteMessage.cs


示例16: WriteBodyTo

 // internal methods
 internal override void WriteBodyTo(BsonStreamWriter streamWriter)
 {
     using (var bsonWriter = new BsonBinaryWriter(streamWriter.BaseStream, WriterSettings))
     {
         bsonWriter.PushMaxDocumentSize(_maxDocumentSize);
         if (_query == null)
         {
             bsonWriter.WriteStartDocument();
             bsonWriter.WriteEndDocument();
         }
         else
         {
             BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, b => b.SerializeIdFirst = true);
         }
         bsonWriter.PopMaxDocumentSize();
     }
 }
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:18,代码来源:MongoDeleteMessage.cs


示例17: WriteBody

        // protected methods
        protected override void WriteBody(BsonBuffer buffer)
        {
            buffer.WriteInt32(0); // reserved
            buffer.WriteCString(_collectionFullName);
            buffer.WriteInt32((int)_flags);

            using (var bsonWriter = new BsonBinaryWriter(buffer, false, WriterSettings))
            {
                if (_query == null)
                {
                    bsonWriter.WriteStartDocument();
                    bsonWriter.WriteEndDocument();
                }
                else
                {
                    BsonSerializer.Serialize(bsonWriter, _query.GetType(), _query, DocumentSerializationOptions.SerializeIdFirstInstance);
                }
            }
        }
开发者ID:wireclub,项目名称:mongo-csharp-driver,代码行数:20,代码来源:MongoDeleteMessage.cs


示例18: BackpatchSize_should_throw_when_size_is_larger_than_2GB

        public void BackpatchSize_should_throw_when_size_is_larger_than_2GB()
        {
            using (var stream = new NullBsonStream())
            using (var binaryWriter = new BsonBinaryWriter(stream))
            {
                var bytes = new byte[int.MaxValue / 2]; // 1GB
                var binaryData = new BsonBinaryData(bytes);

                binaryWriter.WriteStartDocument();
                binaryWriter.WriteName("array");
                binaryWriter.WriteStartArray();
                binaryWriter.WriteBinaryData(binaryData);
                binaryWriter.WriteBinaryData(binaryData);

                Action action = () => binaryWriter.WriteEndArray(); // indirectly calls private BackpatchSize method

                action.ShouldThrow<FormatException>();
            }
        }
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:19,代码来源:BsonBinaryWriterTests.cs


示例19: SerializeRequest

            // protected methods
            protected override void SerializeRequest(BsonBinaryWriter bsonWriter, WriteRequest request)
            {
                var updateRequest = (UpdateRequest)request;

                bsonWriter.PushMaxDocumentSize(MaxWireDocumentSize);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("q");
                BsonSerializer.Serialize(bsonWriter, updateRequest.Query ?? new QueryDocument());
                bsonWriter.WriteName("u");
                BsonSerializer.Serialize(bsonWriter, updateRequest.Update);
                if (updateRequest.IsMultiUpdate.HasValue)
                {
                    bsonWriter.WriteBoolean("multi", updateRequest.IsMultiUpdate.Value);
                }
                if (updateRequest.IsUpsert.HasValue)
                {
                    bsonWriter.WriteBoolean("upsert", updateRequest.IsUpsert.Value);
                }
                bsonWriter.WriteEndDocument();
                bsonWriter.PopMaxDocumentSize();
            }
开发者ID:GGsus,项目名称:mongo-csharp-driver,代码行数:22,代码来源:BulkUpdateOperation.cs


示例20: BsonBinaryWriter_should_support_writing_multiple_documents

        public void BsonBinaryWriter_should_support_writing_multiple_documents(
            [Range(0, 3)]
            int numberOfDocuments)
        {
            var document = new BsonDocument("x", 1);
            var bson = document.ToBson();
            var expectedResult = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();

            using (var stream = new MemoryStream())
            using (var binaryWriter = new BsonBinaryWriter(stream))
            {
                for (var n = 0; n < numberOfDocuments; n++)
                {
                    binaryWriter.WriteStartDocument();
                    binaryWriter.WriteName("x");
                    binaryWriter.WriteInt32(1);
                    binaryWriter.WriteEndDocument();
                }

                var result = stream.ToArray();
                result.Should().Equal(expectedResult);
            }
        }
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:23,代码来源:BsonBinaryWriterTests.cs



注:本文中的MongoDB.Bson.IO.BsonBinaryWriter类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# IO.BsonBinaryWriterContext类代码示例发布时间:2022-05-26
下一篇:
C# IO.BsonBinaryReaderContext类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap