Skip to content

Instantly share code, notes, and snippets.

@mbrookson
Created February 5, 2021 11:54
Show Gist options
  • Save mbrookson/e05aa3898aae3e97ca762b5dba38534e to your computer and use it in GitHub Desktop.
Save mbrookson/e05aa3898aae3e97ca762b5dba38534e to your computer and use it in GitHub Desktop.
.NET Core alphabetical property name JSON serializer
public class AlphabeticalOrderJsonContractResolver : CamelCasePropertyNamesContractResolver
{
protected override IList<JsonProperty> CreateProperties(
Type type,
MemberSerialization memberSerialization
)
{
return base.CreateProperties(type, memberSerialization)
.OrderBy(p => p.PropertyName)
.ToList();
}
}
// Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddNewtonsoftJson(
options =>
{
options.SerializerSettings.ContractResolver = new AlphabeticalOrderJsonContractResolver();
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment