在迁移到 .NET Core 3 时,我已从 Newtonsoft.Json 序列化切换到 System.Text.Json.Serialization。在所有功能中,我想继续使用 JsonPropertyName 属性。
允许的 Newtonsoft 版本 ordering of serialized attributes :
[JsonProperty(Order = 1)]
public bool Deleted { get; set; }
[JsonProperty(Order = 2)]
public DateTime DeletedDate { get; set; }
有没有办法在 System.Text.Json.Serialization 中实现相同的目的?
最佳答案
它在 .Net 6 及更高版本中受支持,使用 JsonPropertyOrderAttribute
:
JsonPropertyOrderAttribute Class
Specifies the property order that is present in the JSON when serializing. Lower values are serialized first. If the attribute is not specified, the default value is 0.
If multiple properties have the same value, the ordering is undefined between them.
该属性可以是 applied例如如下:
[JsonPropertyOrder(order : 1)]
关于c# - .NET core 3 : Order of serialization for JsonPropertyName (System. 文本.Json.序列化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59134564/