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


C# BsonDocument.ToBson方法代码示例

本文整理汇总了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()));
        }
开发者ID:stefanocastriotta,项目名称:mongo-csharp-driver,代码行数:29,代码来源:CSharp1460Tests.cs

示例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);
 }
开发者ID:kenegozi,项目名称:mongo-csharp-driver,代码行数:7,代码来源:CommandResultTests.cs

示例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);
 }
开发者ID:kenegozi,项目名称:mongo-csharp-driver,代码行数:7,代码来源:CommandResultTests.cs

示例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);
 }
开发者ID:kenegozi,项目名称:mongo-csharp-driver,代码行数:7,代码来源:CommandResultTests.cs

示例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));
 }
开发者ID:jenrom,项目名称:mongo-csharp-driver,代码行数:7,代码来源:BsonExtensionMethodsTests.cs

示例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);
            }
        }
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:28,代码来源:BsonBinaryReaderTests.cs

示例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));
                }
            }
        }
开发者ID:kamaradclimber,项目名称:mongo-csharp-driver,代码行数:28,代码来源:CSharp71Tests.cs

示例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();
        }
开发者ID:horizon3d,项目名称:SequoiaDB,代码行数:31,代码来源:SDBMessageHelper.cs

示例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>();
        }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:27,代码来源:PartiallyRawBsonDocumentSerializerTests.cs

示例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);
 }
开发者ID:redforks,项目名称:mongo-csharp-driver,代码行数:8,代码来源:BsonRoundTripTests.cs

示例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);
 }
开发者ID:redforks,项目名称:mongo-csharp-driver,代码行数:8,代码来源:BsonRoundTripTests.cs

示例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);
     }
 }
开发者ID:einaregilsson,项目名称:mongo-csharp-driver,代码行数:10,代码来源:LazyBsonDocumentTests.cs

示例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());
        }
开发者ID:redforks,项目名称:mongo-csharp-driver,代码行数:10,代码来源:BsonBufferValueStraddlesChunksTests.cs

示例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());
        }
开发者ID:redforks,项目名称:mongo-csharp-driver,代码行数:10,代码来源:BsonBufferValueStraddlesChunksTests.cs

示例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));
     }
 }
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:10,代码来源:RawBsonDocumentTests.cs


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