-Reorganized examples
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Documentation.Examples
|
||||
{
|
||||
public class CustomContractResolver
|
||||
{
|
||||
public class DynamicContractResolver : DefaultContractResolver
|
||||
{
|
||||
private readonly char _startingWithChar;
|
||||
public DynamicContractResolver(char startingWithChar)
|
||||
{
|
||||
_startingWithChar = startingWithChar;
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
// only serializer properties that start with the specified character
|
||||
properties =
|
||||
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
|
||||
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
public class Person
|
||||
{
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
|
||||
public string FullName
|
||||
{
|
||||
get { return FirstName + " " + LastName; }
|
||||
}
|
||||
}
|
||||
|
||||
public void Example()
|
||||
{
|
||||
Person person = new Person
|
||||
{
|
||||
FirstName = "Dennis",
|
||||
LastName = "Deepwater-Diver"
|
||||
};
|
||||
|
||||
string startingWithF = JsonConvert.SerializeObject(person, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('F') });
|
||||
|
||||
Console.WriteLine(startingWithF);
|
||||
// {
|
||||
// "FirstName": "Dennis",
|
||||
// "FullName": "Dennis Deepwater-Diver"
|
||||
// }
|
||||
|
||||
string startingWithL = JsonConvert.SerializeObject(person, Formatting.Indented,
|
||||
new JsonSerializerSettings { ContractResolver = new DynamicContractResolver('L') });
|
||||
|
||||
Console.WriteLine(startingWithL);
|
||||
// {
|
||||
// "LastName": "Deepwater-Diver"
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user