本文整理汇总了C#中Newtonsoft.Json.Tests.TestObjects.Person类的典型用法代码示例。如果您正苦于以下问题:C# Person类的具体用法?C# Person怎么用?C# Person使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Person类属于Newtonsoft.Json.Tests.TestObjects命名空间,在下文中一共展示了Person类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: JsonConvertSerializerSettings
public void JsonConvertSerializerSettings()
{
Person person = new Person();
person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
person.Name = "Name!";
string json = JsonConvert.SerializeObject(person, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Assert.AreEqual(@"{
""name"": ""Name!"",
""birthDate"": ""2000-11-20T23:55:44Z"",
""lastModified"": ""2000-11-20T23:55:44Z""
}", json);
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
Assert.AreEqual(person.Name, deserializedPerson.Name);
json = JsonConvert.SerializeObject(person, Formatting.Indented);
Assert.AreEqual(@"{
""Name"": ""Name!"",
""BirthDate"": ""2000-11-20T23:55:44Z"",
""LastModified"": ""2000-11-20T23:55:44Z""
}", json);
}
示例2: PopulatePerson
public void PopulatePerson()
{
Person p = new Person();
JsonConvert.PopulateObject(@"{""Name"":""James""}", p);
Assert.AreEqual("James", p.Name);
}
示例3: CreateGetWithBadObjectTarget
public void CreateGetWithBadObjectTarget()
{
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p);
}
开发者ID:adrianhewitt,项目名称:to-Screenshot-Generator,代码行数:9,代码来源:DynamicReflectionDelegateFactoryTests.cs
示例4: CreateGetWithBadObjectTarget
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>("Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
() =>
{
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
setter(p);
});
}
示例5: CreateSetWithBadObjectTarget
public void CreateSetWithBadObjectTarget()
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
}
开发者ID:adrianhewitt,项目名称:to-Screenshot-Generator,代码行数:13,代码来源:DynamicReflectionDelegateFactoryTests.cs
示例6: BlogCodeSample
public void BlogCodeSample()
{
Person p = new Person
{
Name = "Keith",
BirthDate = new DateTime(1980, 3, 8),
LastModified = new DateTime(2009, 4, 12, 20, 44, 55),
};
string jsonText = JsonConvert.SerializeObject(p, new IsoDateTimeConverter());
// {
// "Name": "Keith",
// "BirthDate": "1980-03-08T00:00:00",
// "LastModified": "2009-04-12T20:44:55"
// }
Assert.AreEqual(@"{""Name"":""Keith"",""BirthDate"":""1980-03-08T00:00:00"",""LastModified"":""2009-04-12T20:44:55""}", jsonText);
}
示例7: BlogCodeSample
public void BlogCodeSample()
{
Person p = new Person
{
Name = "Keith",
BirthDate = new DateTime(1980, 3, 8),
LastModified = new DateTime(2009, 4, 12, 20, 44, 55),
};
string jsonText = JsonConvert.SerializeObject(p, new IsoDateTimeConverter());
// {
// "Name": "Keith",
// "BirthDate": "1980-03-08T00:00:00",
// "LastModified": "2009-04-12T20:44:55"
// }
Console.WriteLine(jsonText);
}
示例8: FromObjectInsideConverterWithCustomSerializer
public void FromObjectInsideConverterWithCustomSerializer()
{
var p = new Person
{
Name = "Daniel Wertheim",
};
var settings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> { new FooJsonConverter() },
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var json = JsonConvert.SerializeObject(p, settings);
Assert.AreEqual(@"{""foo"":""bar"",""name"":""Daniel Wertheim"",""birthDate"":""0001-01-01T00:00:00"",""lastModified"":""0001-01-01T00:00:00""}", json);
}
示例9: CreatePropertyGetter
public void CreatePropertyGetter()
{
PropertyInfo namePropertyInfo = typeof(Person).GetProperty(nameof(Person.Name));
Assert.IsNotNull(namePropertyInfo);
var call = DynamicReflectionDelegateFactory.Instance.CreateGet<Person>(namePropertyInfo);
Person p = new Person();
p.Name = "Name!";
object result = call(p);
Assert.IsNotNull(result);
Assert.AreEqual("Name!", (string)result);
}
示例10: CreateSetWithBadObjectTarget
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>(() =>
{
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
}, "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.");
}
示例11: CreateSetWithBadObjectTarget
public void CreateSetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>(
() => {
Person p = new Person();
Movie m = new Movie();
Action<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
setter(m, "Hi");
Assert.AreEqual(m.Name, "Hi");
setter(p, "Hi");
Assert.AreEqual(p.Name, "Hi");
},
new [] {
"Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
"Cannot cast from source type to destination type." // mono
});
}
示例12: PersonPropertyClass
public PersonPropertyClass()
{
Person = new WagePerson();
}
示例13: CreateGetWithBadObjectTarget
public void CreateGetWithBadObjectTarget()
{
ExceptionAssert.Throws<InvalidCastException>(
() => {
Person p = new Person();
p.Name = "Hi";
Func<object, object> setter = ExpressionReflectionDelegateFactory.Instance.CreateGet<object>(TestReflectionUtils.GetProperty(typeof(Movie), "Name"));
setter(p);
},
new [] {
"Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.",
"Cannot cast from source type to destination type." // mono
});
}
示例14: JsonConvertSerializerSettings
public void JsonConvertSerializerSettings()
{
var person = new Person();
person.BirthDate = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
person.LastModified = new DateTime(2000, 11, 20, 23, 55, 44, DateTimeKind.Utc);
person.Name = "Name!";
string json = ChasonSerializer.SerializeToString(person, new ChasonSerializerSettings()
{
PropertyNameComparer = StringComparer.OrdinalIgnoreCase,
OutputFormattedJson = true,
OutputCamelCasePropertyNames = true,
DateTimeFormat = ChasonSerializerSettings.JavascriptDateObjectDateTimeFormat
});
json.Should().Be(@"{
""name"": ""Name!"",
""birthDate"": ""\/Date(974764544000)\/"",
""lastModified"": ""\/Date(974764544000)\/""
}");
Person deserializedPerson = ChasonSerializer.DeserializeFromString<Person>(json, new ChasonSerializerSettings
{
PropertyNameComparer = StringComparer.OrdinalIgnoreCase,
OutputFormattedJson = true,
OutputCamelCasePropertyNames = true,
DateTimeFormat = ChasonSerializerSettings.JavascriptDateObjectDateTimeFormat
});
Assert.AreEqual(person.BirthDate, deserializedPerson.BirthDate);
Assert.AreEqual(person.LastModified, deserializedPerson.LastModified);
Assert.AreEqual(person.Name, deserializedPerson.Name);
json = ChasonSerializer.SerializeToString(person, new ChasonSerializerSettings() { OutputFormattedJson = true });
json.Should().Be(@"{
""Name"": ""Name!"",
""BirthDate"": ""\/Date(974764544000)\/"",
""LastModified"": ""\/Date(974764544000)\/""
}");
}