本文整理匯總了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)\/""
}");
}