Skip to content

Latest commit

 

History

History
66 lines (57 loc) · 1.92 KB

JsonPropertyItemLevelSetting.md

File metadata and controls

66 lines (57 loc) · 1.92 KB

JsonPropertyAttribute items setting

This sample uses Argon.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.

public class Business
{
    public string Name { get; set; }

    [JsonProperty(ItemIsReference = true)] public IList<Employee> Employees { get; set; }
}

public class Employee
{
    public string Name { get; set; }

    [JsonProperty(IsReference = true)] public Employee Manager { get; set; }
}

snippet source | anchor

var manager = new Employee
{
    Name = "George-Michael"
};
var worker = new Employee
{
    Name = "Maeby",
    Manager = manager
};

var business = new Business
{
    Name = "Acme Ltd.",
    Employees = [manager, worker]
};

var json = JsonConvert.SerializeObject(business, Formatting.Indented);

Console.WriteLine(json);
// {
//   "Name": "Acme Ltd.",
//   "Employees": [
//     {
//       "$id": "1",
//       "Name": "George-Michael",
//       "Manager": null
//     },
//     {
//       "$id": "2",
//       "Name": "Maeby",
//       "Manager": {
//         "$ref": "1"
//       }
//     }
//   ]
// }

snippet source | anchor