Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 1.75 KB

JsonPropertyOrder.md

File metadata and controls

53 lines (44 loc) · 1.75 KB

JsonPropertyAttribute order

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 = "[email protected]",
    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": "[email protected]",
//   "CreatedDate": "2010-10-01T00:00:00",
//   "UpdatedDate": "2013-01-25T00:00:00",
//   "Deleted": true,
//   "DeletedDate": "2013-01-25T00:00:00"
// }

snippet source | anchor