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


C# BsonDocument.ToJson方法代码示例

本文整理汇总了C#中BsonDocument.ToJson方法的典型用法代码示例。如果您正苦于以下问题:C# BsonDocument.ToJson方法的具体用法?C# BsonDocument.ToJson怎么用?C# BsonDocument.ToJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BsonDocument的用法示例。


在下文中一共展示了BsonDocument.ToJson方法的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: TestDateTime

 public void TestDateTime()
 {
     DateTime jan_1_2010 = new DateTime(2010, 1, 1);
     BsonDocument document = new BsonDocument() {
         { "date", jan_1_2010 }
     };
     var settings = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.Strict };
     string json = document.ToJson(settings);
     string expected = "{ \"date\" : { \"$date\" : 1262322000000 } }";
     Assert.AreEqual(expected, json);
     settings = new BsonJsonWriterSettings { OutputMode = BsonJsonOutputMode.JavaScript };
     json = document.ToJson(settings);
     expected = "{ \"date\" : Date(1262322000000) }";
     Assert.AreEqual(expected, json);
 }
开发者ID:abolibibelot,项目名称:mongo-csharp-driver,代码行数:15,代码来源:BsonJsonWriterTests.cs

示例3: TestDateTime

 public void TestDateTime()
 {
     DateTime jan_1_2010 = new DateTime(2010, 1, 1);
     double expectedValue = (jan_1_2010.ToUniversalTime() - BsonConstants.UnixEpoch).TotalMilliseconds;
     BsonDocument document = new BsonDocument() {
         { "date", jan_1_2010 }
     };
     var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
     string json = document.ToJson(settings);
     string expected = "{ \"date\" : { \"$date\" : # } }".Replace("#", expectedValue.ToString());
     Assert.AreEqual(expected, json);
     settings = new JsonWriterSettings { OutputMode = JsonOutputMode.JavaScript };
     json = document.ToJson(settings);
     expected = "{ \"date\" : Date(#) }".Replace("#", expectedValue.ToString());
     Assert.AreEqual(expected, json);
 }
开发者ID:modesto,项目名称:mongo-csharp-driver,代码行数:16,代码来源:JsonWriterTests.cs

示例4: ParseResource

 public static Resource ParseResource(BsonDocument document)
 {
     RemoveMetadata(document);
     string json = document.ToJson();
     Resource resource = FhirParser.ParseResourceFromJson(json);
     return resource;
 }
开发者ID:Condeti,项目名称:spark,代码行数:7,代码来源:BsonHelper.cs

示例5: ExportSalesByProductReport

        public static void ExportSalesByProductReport(IList<SalesReport> reports)
        {
            const string path = @"..\..\Exported-Files\Json-Reports\";

            var client = new MongoClient();
            var database = client.GetDatabase("Reports");
            database.DropCollectionAsync("SalesByProductReports");
            var collection = database.GetCollection<BsonDocument>("SalesByProductReports");

            ClearDirectory(path);
            foreach (var report in reports)
            {
                var currentReport = new BsonDocument
                {
                    { "product-id", report.ProductId },
                    { "product-name", report.ProductName },
                    { "vendor-name", report.VendorName },
                    { "total-quantity-sold", report.TotalQuantitySold },
                    { "total-incomes", report.TotalIncomes.ToString() }
                };

                File.WriteAllText(path + report.ProductId + ".json", currentReport.ToJson());
                collection.InsertOneAsync(currentReport).Wait();
            }
        }
开发者ID:Martin-Andreev,项目名称:Team-Billy_Buttons-SupermarketsChain,代码行数:25,代码来源:ExportToJSON.cs

示例6: AddBsonArray

 public void AddBsonArray()
 {
     var person = new BsonDocument();
     person.Add("Name", new BsonString("Nameirakpam"));
     person.Add("address", new BsonArray(new[] {"Swapanalok","D-504","Pade Basti","Phursungi","Hadapsar"}));
     Console.WriteLine(person.ToJson());
 }
开发者ID:nameinan,项目名称:realstate,代码行数:7,代码来源:BsonDocumentTest.cs

示例7: Run

        public void Run()
        {
            var doc = new BsonDocument();
            IDictionary<string,string> seen = new Dictionary<string,string>(StringComparer.InvariantCultureIgnoreCase);

            foreach(var dir in Directory.EnumerateDirectories(".", "*", SearchOption.AllDirectories).Where(d=>!Ignore(d)))
            {
                var directoryName = dir;

                var assemblyName = Path.GetFileName(dir);
                if (assemblyName == null) continue;

                var nuspecFile = Path.Combine(dir, assemblyName + ".nuspec");
                if(!File.Exists(nuspecFile)) continue;

                const string s = ".\\";
                if (directoryName.StartsWith(s))
                    directoryName = directoryName.Substring(s.Length);

                if (seen.ContainsKey(assemblyName))
                {
                    _out.WriteLine("Conflict: {0} in both '{1}' and '{2}'", assemblyName, directoryName, seen[assemblyName]);
                }
                seen[assemblyName] = directoryName;
                doc[assemblyName] = directoryName;
            }
            File.WriteAllText("source_index.nugetine.json", doc.ToJson(_settings));
        }
开发者ID:tanglebones,项目名称:ch-nugetine,代码行数:28,代码来源:SourceIndexer.cs

示例8: TestInt64TenGen

 public void TestInt64TenGen() {
     var document = new BsonDocument { { "a", 1L } };
     var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.TenGen };
     var json = document.ToJson(settings);
     var expected = "{ 'a' : NumberLong(1) }".Replace("'", "\"");
     Assert.AreEqual(expected, json);
 }
开发者ID:ebix,项目名称:mongo-csharp-driver,代码行数:7,代码来源:JsonWriterTests.cs

示例9: TestIndentedTwoElements

 public void TestIndentedTwoElements() {
     BsonDocument document = new BsonDocument() { { "a", "x" }, { "b", "y" } };
     var settings = new JsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{\r\n  \"a\" : \"x\",\r\n  \"b\" : \"y\"\r\n}";
     Assert.AreEqual(expected, json);
 }
开发者ID:ebix,项目名称:mongo-csharp-driver,代码行数:7,代码来源:JsonWriterTests.cs

示例10: TestIndentedOneElement

 public void TestIndentedOneElement() {
     BsonDocument document = new BsonDocument() { { "name", "value" } };
     var settings = new JsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{\r\n  \"name\" : \"value\"\r\n}";
     Assert.AreEqual(expected, json);
 }
开发者ID:ebix,项目名称:mongo-csharp-driver,代码行数:7,代码来源:JsonWriterTests.cs

示例11: TestIndentedEmptyDocument

 public void TestIndentedEmptyDocument() {
     BsonDocument document = new BsonDocument();
     var settings = new JsonWriterSettings { Indent = true };
     string json = document.ToJson(settings);
     string expected = "{ }";
     Assert.AreEqual(expected, json);
 }
开发者ID:ebix,项目名称:mongo-csharp-driver,代码行数:7,代码来源:JsonWriterTests.cs

示例12: JsonReader_should_support_reading_multiple_documents

        public void JsonReader_should_support_reading_multiple_documents(
            [Range(0, 3)]
            int numberOfDocuments)
        {
            var document = new BsonDocument("x", 1);
            var json = document.ToJson();
            var input = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j);
            var expectedResult = Enumerable.Repeat(document, numberOfDocuments);

            using (var jsonReader = new JsonReader(input))
            {
                var result = new List<BsonDocument>();

                while (!jsonReader.IsAtEndOfFile())
                {
                    jsonReader.ReadStartDocument();
                    var name = jsonReader.ReadName();
                    var value = jsonReader.ReadInt32();
                    jsonReader.ReadEndDocument();

                    var resultDocument = new BsonDocument(name, value);
                    result.Add(resultDocument);
                }

                result.Should().Equal(expectedResult);
            }
        }
开发者ID:mfidemraizer,项目名称:mongo-csharp-driver,代码行数:27,代码来源:JsonReaderTests.cs

示例13: TestAddHashtableWithOneEntry

 public void TestAddHashtableWithOneEntry()
 {
     var hashtable = new Hashtable { { "A", 1 } };
     var document = new BsonDocument(hashtable);
     var json = document.ToJson();
     var expected = "{ 'A' : 1 }".Replace("'", "\"");
     Assert.AreEqual(expected, json);
 }
开发者ID:jijamw,项目名称:mongo-csharp-driver,代码行数:8,代码来源:BsonDocumentTests.cs

示例14: TestBinary

 public void TestBinary()
 {
     var document = new BsonDocument {
         { "bin", new BsonBinaryData(new byte[] { 1, 2, 3 }) }
     };
     string expected = "{ \"bin\" : { \"$binary\" : \"AQID\", \"$type\" : \"00\" } }";
     string actual = document.ToJson();
     Assert.AreEqual(expected, actual);
 }
开发者ID:modesto,项目名称:mongo-csharp-driver,代码行数:9,代码来源:JsonWriterTests.cs

示例15: TestArray

 public void TestArray()
 {
     BsonDocument document = new BsonDocument() {
         { "array", new BsonArray { 1, 2, 3 } }
     };
     string json = document.ToJson();
     string expected = "{ \"array\" : [1, 2, 3] }";
     Assert.AreEqual(expected, json);
 }
开发者ID:modesto,项目名称:mongo-csharp-driver,代码行数:9,代码来源:JsonWriterTests.cs


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