本文整理匯總了C#中System.Test.ToJson方法的典型用法代碼示例。如果您正苦於以下問題:C# Test.ToJson方法的具體用法?C# Test.ToJson怎麽用?C# Test.ToJson使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Test
的用法示例。
在下文中一共展示了Test.ToJson方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TestClassMap
public void TestClassMap()
{
// this test passes normally when the Test class is automapped
// uncomment all or parts of the class map initialization code to test
// the exceptions thrown for each non-compliant field or property
if (firstTime) {
BsonClassMap.RegisterClassMap<Test>(cm => {
cm.AutoMap();
// cm.MapField("literal");
// cm.MapField("readOnly");
// cm.MapField("notfound");
// cm.MapProperty("GetOnly");
// cm.MapProperty("SetOnly");
// cm.MapProperty("notfound");
// cm.MapMember(null);
});
firstTime = false;
}
var test = new Test("x") { SetOnly = "y" };
var json = test.ToJson();
var expected = "{ '_id' : { '$oid' : '000000000000000000000000' } }".Replace("'", "\"");
// Assert.AreEqual(expected, json);
var bson = test.ToBson();
var rehydrated = BsonSerializer.Deserialize<Test>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}
示例2: TestClassMap
public void TestClassMap() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
Assert.AreEqual(2, classMap.MemberMaps.Count());
Assert.IsTrue(classMap.MemberMaps.Any(m => m.MemberName == "Id"));
Assert.IsTrue(classMap.MemberMaps.Any(m => m.MemberName == "Normal"));
Assert.AreEqual("Id", classMap.IdMemberMap.MemberName);
var test = new Test { Normal = "normal" };
var json = test.ToJson();
var expected = "{ '_id' : { '$oid' : '000000000000000000000000' }, 'Normal' : 'normal' }".Replace("'", "\"");
Assert.AreEqual(expected, json);
var bson = test.ToBson();
var rehydrated = BsonSerializer.Deserialize<Test>(bson);
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson()));
}