There are two ways to serialize an object’s properties to JSON in alphabetical order using System.Text.Json:
- Manually alphabetize the properties and apply the JsonPropertyOrder attribute.
- Automatically sort properties alphabetically with a contract resolver.
I’ll show how to do these two options below.
Option 1 – Manually alphabetize with JsonPropertyOrder
You can specify the exact serialization ordering by using the JsonPropertyOrder attribute. Therefore, to serialize in alphabetical order, first arrange the properties in alphabetical order. Then apply the JsonPropertyOrder attribute with values in ascending order. Here’s an example:
using System.Text.Json.Serialization;
public class Person
{
[JsonPropertyOrder(1)]
public int Age { get; set; }
[JsonPropertyOrder(2)]
public DateTime BirthDate { get; set; }
[JsonPropertyOrder(3)]
public string Career { get; set; }
}
Code language: C# (cs)
using System.Text.Json;
var person = new Person()
{
Age = 23,
BirthDate = new DateTime(2000, 7, 24),
Career = "Coder"
};
var options = new JsonSerializerOptions() { WriteIndented = true };
var json = JsonSerializer.Serialize(person, options);
Console.WriteLine(json);
Code language: C# (cs)
This outputs the following JSON. Notice the properties are in alphabetical order:
{
"Age": 23,
"BirthDate": "2000-07-24T00:00:00",
"Career": "Coder"
}
Code language: JSON / JSON with Comments (json)
This is a good option if you’re only dealing with a few classes and want a simple, direct approach. Otherwise, use the automatic approach, which I’ll show next.
Option 2 – Serialize alphabetically with a contract resolver
You can create a custom contract resolver (added to System.Text.Json in v7) to serialize properties alphabetically.
First, implement a custom contract resolver:
- Subclass DefaultJsonTypeInfoResolver and override GetTypeInfo().
- Get the default JsonTypeInfo with base.GetTypeInfo().
- Loop through JsonTypeInfo.Properties in sorted order (sort with Linq method OrderBy()).
- For each property, set JsonPropertyInfo.Order to an incrementing value.
This effectively changes the serialization order of the properties to alphabetical order. The following code shows how to implement this custom contract resolver:
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
public class AlphabeticalOrderContractResolver : DefaultJsonTypeInfoResolver
{
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
{
var jsonTypeInfo = base.GetTypeInfo(type, options);
int order = 1;
foreach(var property in jsonTypeInfo.Properties.OrderBy(p => p.Name))
{
property.Order = order++;
}
return jsonTypeInfo;
}
}
Code language: C# (cs)
To use this during serialization, set JsonSerializerOptions.TypeInfoResolver. Here’s an example:
using System.Text.Json;
var person = new Person()
{
Age = 23,
BirthDate = new DateTime(2000, 7, 24),
Career = "Coder"
};
var options = new JsonSerializerOptions() { WriteIndented = true };
options.TypeInfoResolver = new AlphabeticalOrderContractResolver();
var json = JsonSerializer.Serialize(person, options);
Console.WriteLine(json);
Code language: C# (cs)
This outputs the following JSON. Notice the properties are in alphabetical order.
{
"Age": 23,
"BirthDate": "2000-07-24T00:00:00",
"Career": "Coder"
}
Code language: JSON / JSON with Comments (json)
This feature was added in System.Text.Json v7. To get this feature in a previous version of .NET, you can install the latest System.Text.Json package. Otherwise:
- Write a custom JSON converter that sorts properties alphabetically before serializing -OR-
- Use Newtonsoft to serialize alphabetically.