本文整理汇总了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()));
}
示例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);
}
示例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);
}
示例4: ParseResource
public static Resource ParseResource(BsonDocument document)
{
RemoveMetadata(document);
string json = document.ToJson();
Resource resource = FhirParser.ParseResourceFromJson(json);
return resource;
}
示例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();
}
}
示例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());
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}