问题
When serializing objects using the Netwonsoft.JSON library, it's possible to specify the output order using JsonPropertyAttribute
property Order
. However, I would like to also sort alphabetically the properties by default on top of that.
回答1:
You can actually control the order by implementing IContractResolver
or overriding the DefaultContractResolver's CreateProperties
method.
Here's an example of my simple implementation of IContractResolver
which orders the properties alphabetically:
public class OrderedContractResolver : DefaultContractResolver
{
protected override System.Collections.Generic.IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).OrderBy(p => p.PropertyName).ToList();
}
}
And then set the settings and serialize the object, and the JSON
fields will be in alphabetical order:
var settings = new JsonSerializerSettings()
{
ContractResolver = new OrderedContractResolver()
};
var json = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
回答2:
You can create a custom contract resolver, by extending Newtonsoft.Json.Serialization.DefaultContractResolver
. The CreateProperties
method is the one responsible of the property order, so overriding it, and re-sorting the properties would change the behaviour in the way you want:
public class OrderedContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override System.Collections.Generic.IList<Newtonsoft.Json.Serialization.JsonProperty> CreateProperties(System.Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
{
var @base = base.CreateProperties(type, memberSerialization);
var ordered = @base
.OrderBy(p => p.Order ?? int.MaxValue)
.ThenBy(p => p.PropertyName)
.ToList();
return ordered;
}
}
In order to use a custom contract resolver you have to create a custom Newtonsoft.Json.JsonSerializerSettings
and set its ContractResolver
to an instance of it:
var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings
{
ContractResolver = new OrderedContractResolver(),
};
and then serialize using the above settings object's instance:
using (Newtonsoft.Json.JsonWriter writer = new Newtonsoft.Json.JsonTextWriter(sw))
{
var serializer = Newtonsoft.Json.JsonSerializer.Create(jsonSerializerSettings);
serializer.Serialize(writer, jsonObject);
}
where sw
is a simple string writer:
var sb = new System.Text.StringBuilder();
var sw = new System.IO.StringWriter(sb);
and jsonObject
is the object you wish to serialize.
回答3:
You could define your own ContractResolver:
public class CustomContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization).OrderBy(x=>x.PropertyName).ToList();
}
}
With usage:
string json = JsonConvert.SerializeObject(myClass, new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver(),
Formatting = Formatting.Indented
});
来源:https://stackoverflow.com/questions/56933494/how-to-sort-properties-alphabetically-when-serializing-json-using-netwonsoft-lib