当前位置: 首页>>代码示例>>C#>>正文


C# IO.BsonBinaryWriter类代码示例

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


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


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