本文整理汇总了C#中Parent.ToJson方法的典型用法代码示例。如果您正苦于以下问题:C# Parent.ToJson方法的具体用法?C# Parent.ToJson怎么用?C# Parent.ToJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Parent
的用法示例。
在下文中一共展示了Parent.ToJson方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test
public void Test()
{
var p = new Parent { Child = new Child() };
p.Child.A = 1;
var json = p.ToJson();
BsonSerializer.Deserialize<Parent>(json); // throws Unexpected element exception
}
示例2: Does_call_RawSerializeFn_for_toplevel_types
public void Does_call_RawSerializeFn_for_toplevel_types()
{
JsConfig<ICar>.RawSerializeFn = SerializeCar;
var luxaryParent = new Parent() { Car = new LuxaryCar() { Sunroof = "Big" } };
var cheapParent = new Parent() { Car = new CheapCar() { HasCupHolder = true } };
// Works when ICar is a child
var luxaryParentJson = luxaryParent.ToJson();
var cheapParentJson = cheapParent.ToJson();
Assert.That(luxaryParentJson, Is.Not.StringContaining("__type"));
Assert.That(cheapParentJson, Is.Not.StringContaining("__type"));
ICar luxary = new LuxaryCar() { Sunroof = "Big" };
ICar cheap = new CheapCar() { HasCupHolder = true };
// ToJson() loses runtime cast of interface type, to keep it we need to specify it on call-site
var luxaryJson = JsonSerializer.SerializeToString(luxary, typeof(ICar));
var cheapJson = JsonSerializer.SerializeToString(cheap, typeof(ICar));
Assert.That(luxaryJson, Is.Not.StringContaining("__type"));
Assert.That(cheapJson, Is.Not.StringContaining("__type"));
JsConfig.Reset();
}