本文整理汇总了C#中BsonDocument.ToBson方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.ToBson方法的具体用法?C# BsonDocument.ToBson怎么用?C# BsonDocument.ToBson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.ToBson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestJsonWriterLocalDateTimeSetting
public void TestJsonWriterLocalDateTimeSetting()
{
var testDateTime = DateTime.ParseExact("2015-10-28T00:00:00Z", "yyyy-MM-ddTHH:mm:ss.FFFZ", System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime();
var document = new BsonDocument();
document.Add("DateTimeField", testDateTime);
var json = document.ToJson(new Bson.IO.JsonWriterSettings() { UseLocalTime = true });
var expected = ("{ 'DateTimeField' : ISODate('" + testDateTime.ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss.FFFzzz") + "') }").Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = document.ToBson();
var rehydrated = BsonDocument.Parse(json);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
//test without settings, should work as before
json = document.ToJson();
expected = ("{ 'DateTimeField' : ISODate('" + testDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") + "') }").Replace("'", "\"");
Assert.AreEqual(expected, json);
bson = document.ToBson();
rehydrated = BsonDocument.Parse(json);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
//test with parameter, with no setting specified, should work as before
json = document.ToJson(new Bson.IO.JsonWriterSettings());
expected = ("{ 'DateTimeField' : ISODate('" + testDateTime.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ") + "') }").Replace("'", "\"");
Assert.AreEqual(expected, json);
bson = document.ToBson();
rehydrated = BsonDocument.Parse(json);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
示例2: TestErrorMessageMissing
public void TestErrorMessageMissing()
{
var document = new BsonDocument();
var bson = document.ToBson();
var result = BsonSerializer.Deserialize<CommandResult>(bson);
Assert.AreEqual("Unknown error", result.ErrorMessage);
}
示例3: TestErrorMessageNotString
public void TestErrorMessageNotString()
{
var document = new BsonDocument("errmsg", 3.14159);
var bson = document.ToBson();
var result = BsonSerializer.Deserialize<CommandResult>(bson);
Assert.AreEqual("3.14159", result.ErrorMessage);
}
示例4: TestErrorMessagePresent
public void TestErrorMessagePresent()
{
var document = new BsonDocument("errmsg", "An error message");
var bson = document.ToBson();
var result = BsonSerializer.Deserialize<CommandResult>(bson);
Assert.AreEqual("An error message", result.ErrorMessage);
}
示例5: TestToBsonEmptyDocument
public void TestToBsonEmptyDocument()
{
var document = new BsonDocument();
var bson = document.ToBson();
var expected = new byte[] { 5, 0, 0, 0, 0 };
Assert.IsTrue(expected.SequenceEqual(bson));
}
示例6: BsonBinaryReader_should_support_reading_multiple_documents
public void BsonBinaryReader_should_support_reading_multiple_documents(
[Range(0, 3)]
int numberOfDocuments)
{
var document = new BsonDocument("x", 1);
var bson = document.ToBson();
var input = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
using (var stream = new MemoryStream(input))
using (var binaryReader = new BsonBinaryReader(stream))
{
var result = new List<BsonDocument>();
while (!binaryReader.IsAtEndOfFile())
{
binaryReader.ReadStartDocument();
var name = binaryReader.ReadName();
var value = binaryReader.ReadInt32();
binaryReader.ReadEndDocument();
var resultDocument = new BsonDocument(name, value);
result.Add(resultDocument);
}
result.Should().Equal(expectedResult);
}
}
示例7: Test20KDocument
public void Test20KDocument()
{
// manufacture an approximately 20K document using 200 strings each 100 characters long
// it's enough to cause the document to straddle a chunk boundary
var document = new BsonDocument();
var value = new string('x', 100);
for (int i = 0; i < 200; i++)
{
var name = i.ToString();
document.Add(name, value);
}
// round trip tests
var bson = document.ToBson();
var rehydrated = BsonSerializer.Deserialize<BsonDocument>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
// test failure mode when 20 bytes are truncated from the buffer
using (var buffer = new BsonBuffer())
{
buffer.LoadFrom(new MemoryStream(bson));
buffer.Length -= 20;
using (var bsonReader = BsonReader.Create(buffer))
{
Assert.Throws<EndOfStreamException>(() => BsonSerializer.Deserialize<BsonDocument>(bsonReader));
}
}
}
示例8: AppendInsertMsg
internal static byte[] AppendInsertMsg(byte[] msg, BsonDocument append, bool isBigEndian)
{
List<byte[]> tmp = Helper.SplitByteArray(msg, 4);
byte[] msgLength = tmp[0];
byte[] remainning = tmp[1];
byte[] insertor = append.ToBson();
if (isBigEndian)
{
BsonEndianConvert(insertor, 0, insertor.Length, true);
}
int length = Helper.ByteToInt(msgLength, isBigEndian);
int messageLength = length + Helper.RoundToMultipleXLength(insertor.Length, 4);
ByteBuffer buf = new ByteBuffer(messageLength);
if (isBigEndian)
buf.IsBigEndian = true;
buf.PushInt(messageLength);
buf.PushByteArray(remainning);
buf.PushByteArray(Helper.RoundToMultipleX(insertor, 4));
if (logger.IsDebugEnabled)
{
StringWriter buff = new StringWriter();
foreach (byte by in insertor)
{
buff.Write(string.Format("{0:X}", by));
}
logger.Debug("BulkInsert Append string==>" + buff.ToString() + "<==");
}
return buf.ToByteArray();
}
示例9: Deserialize_should_return_nested_partially_raw_BsonDocument
public void Deserialize_should_return_nested_partially_raw_BsonDocument()
{
var document = new BsonDocument
{
{ "a", new BsonDocument("x", 1) },
{ "b", new BsonDocument
{
{ "d", new BsonDocument("z", 1) },
{ "e", new BsonDocument("z", 2) },
{ "f", new BsonDocument("z", 3) },
}
},
{ "c", new BsonDocument("x", 3) }
};
var bson = document.ToBson();
var subject = new PartiallyRawBsonDocumentSerializer("b",
new PartiallyRawBsonDocumentSerializer("e", RawBsonDocumentSerializer.Instance));
var result = Deserialize(bson, subject);
result["a"].Should().BeOfType<BsonDocument>();
result["b"].Should().BeOfType<BsonDocument>();
result["c"].Should().BeOfType<BsonDocument>();
result["b"]["d"].Should().BeOfType<BsonDocument>();
result["b"]["e"].Should().BeOfType<RawBsonDocument>();
result["b"]["f"].Should().BeOfType<BsonDocument>();
}
示例10: TestBsonIsAwesome
public void TestBsonIsAwesome() {
BsonDocument document = new BsonDocument {
{ "BSON", new BsonArray { "awesome", 5.05, 1986} }
};
byte[] bytes1 = document.ToBson();
byte[] bytes2 = BsonDocument.ReadFrom(bytes1).ToBson();
Assert.AreEqual(bytes1, bytes2);
}
示例11: TestHelloWorld
public void TestHelloWorld() {
BsonDocument document = new BsonDocument {
{ "hello", "world" }
};
byte[] bytes1 = document.ToBson();
byte[] bytes2 = BsonDocument.ReadFrom(bytes1).ToBson();
Assert.AreEqual(bytes1, bytes2);
}
示例12: TestAddBsonElement
public void TestAddBsonElement()
{
var bsonDocument = new BsonDocument { { "x", 1 }, { "y", 2 } };
var bson = bsonDocument.ToBson();
using (var lazyBsonDocument = BsonSerializer.Deserialize<LazyBsonDocument>(bson))
{
lazyBsonDocument.Add(new BsonElement("z", 3));
Assert.AreEqual(3, lazyBsonDocument["z"].AsInt32);
}
}
示例13: TestBinaryDataStraddles
public void TestBinaryDataStraddles() {
var document = new BsonDocument {
{ "x", new string('x', filler - 4) },
{ "y", new BsonBinaryData(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }) }
};
var bson = document.ToBson();
var rehydrated = BsonSerializer.Deserialize<BsonDocument>(bson);
Assert.AreEqual(bson, rehydrated.ToBson());
}
示例14: TestArrayLengthStraddles
public void TestArrayLengthStraddles() {
var document = new BsonDocument {
{ "x", new string('x', filler) },
{ "y", new BsonArray { 1, 2, 3, 4 } }
};
var bson = document.ToBson();
var rehydrated = BsonSerializer.Deserialize<BsonDocument>(bson);
Assert.AreEqual(bson, rehydrated.ToBson());
}
示例15: TestContainsValue
public void TestContainsValue()
{
var bsonDocument = new BsonDocument { { "x", 1 }, { "y", 2 } };
var bson = bsonDocument.ToBson();
using (var rawBsonDocument = BsonSerializer.Deserialize<RawBsonDocument>(bson))
{
Assert.Equal(true, rawBsonDocument.ContainsValue(1));
Assert.Equal(false, rawBsonDocument.ContainsValue(3));
}
}