本文整理汇总了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);
}
}
示例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);
}
}
示例3: TestWriteNameThrowsWhenValueIsNull
public void TestWriteNameThrowsWhenValueIsNull()
{
using (var stream = new MemoryStream())
using (var bsonWriter = new BsonBinaryWriter(stream, BsonBinaryWriterSettings.Defaults))
{
Assert.Throws<ArgumentNullException>(() => { bsonWriter.WriteName(null); });
}
}
示例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"); });
}
}
示例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
}
}
}
示例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();
}
}
示例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();
}
}
示例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);
}
示例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();
}
}
示例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));
}
}
示例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();
}
示例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();
}
}
示例13: TestFlushAndClose
public void TestFlushAndClose()
{
var stream = new MemoryStream();
using (var bsonWriter = new BsonBinaryWriter(stream))
{
bsonWriter.WriteStartDocument();
bsonWriter.WriteEndDocument();
bsonWriter.Flush();
bsonWriter.Close();
}
}
示例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());
}
}
示例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();
}
}