本文整理汇总了C#中JsonObject.ReadAsType方法的典型用法代码示例。如果您正苦于以下问题:C# JsonObject.ReadAsType方法的具体用法?C# JsonObject.ReadAsType怎么用?C# JsonObject.ReadAsType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonObject
的用法示例。
在下文中一共展示了JsonObject.ReadAsType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestDataContractJsonSerializerSettings
public void TestDataContractJsonSerializerSettings()
{
TestTypeForSerializerSettings instance = new TestTypeForSerializerSettings
{
BaseRef = new DerivedType(),
Date = AnyInstance.AnyDateTime,
Dict = new Dictionary<string, object>
{
{ "one", 1 },
{ "two", 2 },
{ "two point five", 2.5 },
}
};
JsonObject dict = new JsonObject
{
{ "one", 1 },
{ "two", 2 },
{ "two point five", 2.5 },
};
JsonObject equivalentJsonObject = new JsonObject
{
{ "BaseRef", new JsonObject { { "__type", "DerivedType:NS" } } },
{ "Date", AnyInstance.AnyDateTime },
{ "Dict", dict },
};
JsonObject createdFromType = JsonValueExtensions.CreateFrom(instance) as JsonObject;
Assert.Equal(equivalentJsonObject.ToString(), createdFromType.ToString());
TestTypeForSerializerSettings newInstance = equivalentJsonObject.ReadAsType<TestTypeForSerializerSettings>();
// DISABLED, 198487 - Assert.Equal(instance.Date, newInstance.Date);
Assert.Equal(instance.BaseRef.GetType().FullName, newInstance.BaseRef.GetType().FullName);
Assert.Equal(3, newInstance.Dict.Count);
Assert.Equal(1, newInstance.Dict["one"]);
Assert.Equal(2, newInstance.Dict["two"]);
Assert.Equal(2.5, Convert.ToDouble(newInstance.Dict["two point five"], CultureInfo.InvariantCulture));
}
示例2: ReadAsComplexTypeTest
public void ReadAsComplexTypeTest()
{
JsonObject target = new JsonObject
{
{ "Name", AnyInstance.AnyString },
{ "Age", AnyInstance.AnyInt },
{ "Address", new JsonObject { { "Street", Address.AnyStreet }, { "City", Address.AnyCity }, { "State", Address.AnyState } } },
};
Person person = target.ReadAsType<Person>();
Assert.Equal(AnyInstance.AnyString, person.Name);
Assert.Equal(AnyInstance.AnyInt, person.Age);
Assert.NotNull(person.Address);
Assert.Equal(Address.AnyStreet, person.Address.Street);
Assert.Equal(Address.AnyCity, person.Address.City);
Assert.Equal(Address.AnyState, person.Address.State);
}