本文整理汇总了C#中BsonDocument.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.ToByteArray方法的具体用法?C# BsonDocument.ToByteArray怎么用?C# BsonDocument.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonDocument
的用法示例。
在下文中一共展示了BsonDocument.ToByteArray方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestIterate1
public void TestIterate1()
{
var doc = new BsonDocument();
doc["a"] = "av";
doc["bb"] = 24;
//doc["ccc"] = BsonDocument.ValueOf(new{na1 = 1, nb = "2"});
//doc["d"] = new ObjectId("51b9f3af98195c4600000000");
//17-00-00-00 +4
//02-61-00-03-00-00-00-61-76-00 +10
//10-62-62-00-18-00-00-00 +8
//00 +1
Assert.AreEqual("17-00-00-00-02-61-00-03-00-00-00-61-76-00-10-62-62-00-18-00-00-00-00",
doc.ToDebugDataString());
BsonIterator it = new BsonIterator(doc);
Assert.AreEqual(doc.ToByteArray().Length, it.DocumentLength);
var c = "";
while (it.Next() != BsonType.EOO)
{
c += it.CurrentKey;
}
Assert.AreEqual("abb", c);
it.Dispose();
it = new BsonIterator(doc);
var cnt = 0;
while (it.Next() != BsonType.EOO)
{
BsonValue bv = it.FetchCurrentValue();
Assert.IsNotNull(bv);
if (cnt == 0)
{
Assert.IsTrue(bv.BsonType == BsonType.STRING);
Assert.IsTrue(bv.Key == "a");
Assert.AreEqual("av", bv.Value);
}
if (cnt == 1)
{
Assert.IsTrue(bv.BsonType == BsonType.INT);
Assert.IsTrue(bv.Key == "bb");
Assert.AreEqual(24, bv.Value);
}
cnt++;
}
}
示例2: TestSerialize1
public void TestSerialize1()
{
byte[] bdata;
BsonDocument doc = new BsonDocument();
doc.SetNumber("0", 1);
//0C-00-00-00 len
//10 type
//30-00 key
//01-00-00-00 int val
//00 zero term
bdata = doc.ToByteArray();
Assert.AreEqual("0C-00-00-00-10-30-00-01-00-00-00-00", doc.ToDebugDataString());
Assert.AreEqual(bdata.Length, (int)Convert.ToByte(doc.ToDebugDataString().Substring(0, 2), 16));
BsonDocument doc2 = new BsonDocument(doc.ToByteArray());
Assert.AreEqual(1, doc2.KeysCount);
int c = 0;
foreach (BsonValue bv in doc2)
{
c++;
Assert.IsNotNull(bv);
Assert.AreEqual(BsonType.INT, bv.BsonType);
Assert.AreEqual("0", bv.Key);
Assert.AreEqual(1, bv.Value);
}
Assert.That(c > 0);
doc2.SetNumber("0", 2);
Assert.AreEqual(1, doc2.KeysCount);
object ival = doc2["0"];
Assert.IsInstanceOf(typeof(int), ival);
Assert.AreEqual(2, ival);
doc2.SetNumber("1", Int32.MaxValue);
//13-00-00-00
//10
//30-00
//02-00-00-00
//10-31-00
//FF-FF-FF-7F
//00
Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00",
doc2.ToDebugDataString());
doc2 = new BsonDocument(doc2);
Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00",
doc2.ToDebugDataString());
doc2 = new BsonDocument(doc2.ToByteArray());
Assert.AreEqual("13-00-00-00-10-30-00-02-00-00-00-10-31-00-FF-FF-FF-7F-00",
doc2.ToDebugDataString());
doc = new BsonDocument();
doc["a"] = 1;
Assert.AreEqual("0C-00-00-00-10-61-00-01-00-00-00-00", doc.ToDebugDataString());
}