JsonPropertyAttribute order

April 6, 2024 ยท View on GitHub

This sample uses Argon.JsonPropertyAttribute to order of properties when they are serialized to JSON.

public class Account
{
    public string EmailAddress { get; set; }

    // appear last
    [JsonProperty(Order = 1)] public bool Deleted { get; set; }

    [JsonProperty(Order = 2)] public DateTime DeletedDate { get; set; }

    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }

    // appear first
    [JsonProperty(Order = -2)] public string FullName { get; set; }
}

snippet source | anchor

var account = new Account
{
    FullName = "Aaron Account",
    EmailAddress = "aaron@example.com",
    Deleted = true,
    DeletedDate = new(2013, 1, 25),
    UpdatedDate = new(2013, 1, 25),
    CreatedDate = new(2010, 10, 1)
};

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

Console.WriteLine(json);
// {
//   "FullName": "Aaron Account",
//   "EmailAddress": "aaron@example.com",
//   "CreatedDate": "2010-10-01T00:00:00",
//   "UpdatedDate": "2013-01-25T00:00:00",
//   "Deleted": true,
//   "DeletedDate": "2013-01-25T00:00:00"
// }

snippet source | anchor