Click or drag to resize
Json.NETJsonPropertyAttribute items setting
 

This sample uses JsonPropertyAttribute to change how the property value's items are serialized, e.g. setting ItemIsReference to true on a property with a collection will serialize all the collection's items with reference tracking enabled.

Sample
Types
 1public class Business
 2{
 3    public string Name { get; set; }
 4
 5    [JsonProperty(ItemIsReference = true)]
 6    public IList<Employee> Employees { get; set; }
 7}
 8
 9public class Employee
10{
11    public string Name { get; set; }
12
13    [JsonProperty(IsReference = true)]
14    public Employee Manager { get; set; }
15}
Usage
 1Employee manager = new Employee
 2{
 3    Name = "George-Michael"
 4};
 5Employee worker = new Employee
 6{
 7    Name = "Maeby",
 8    Manager = manager
 9};
10
11Business business = new Business
12{
13    Name = "Acme Ltd.",
14    Employees = new List<Employee>
15    {
16        manager,
17        worker
18    }
19};
20
21string json = JsonConvert.SerializeObject(business, Formatting.Indented);
22
23Console.WriteLine(json);
24// {
25//   "Name": "Acme Ltd.",
26//   "Employees": [
27//     {
28//       "$id": "1",
29//       "Name": "George-Michael",
30//       "Manager": null
31//     },
32//     {
33//       "$id": "2",
34//       "Name": "Maeby",
35//       "Manager": {
36//         "$ref": "1"
37//       }
38//     }
39//   ]
40// }