我正在.NET Core 3.0应用程序中从Newtonsoft.Json迁移到System.Text.Json.我正在尝试忽略错误的值.
I'm migrating from Newtonsoft.Json to System.Text.Json in my .NET Core 3.0 application. I'm trying to ignore false values.
在System.Text.Json中,我找到了忽略空值的选项:
In System.Text.Json I found the option to ignore null values:
JsonSerializerOptions.IgnoreNullValues = true;但是我找不到在System.Text.Json中忽略错误值的选项.
But I cannot find the option to ignore false values in System.Text.Json.
有人知道如何使用System.Text.Json来实现吗?
Does someone know how can this be achieved with System.Text.Json?
或者,如果有人知道相当于Newtonsoft DefaultValueHandling = DefaultValueHandling.Ignore选项的话,那也太棒了.
Or if someone know the equivalent of Newtonsoft DefaultValueHandling = DefaultValueHandling.Ignore option that would be fantastic too.
推荐答案这是在中实现的. Net 5.0 :
支持忽略值类型的默认值
Support ignoring value-type defaults
此版本引入了 JsonIgnoreCondition 枚举:
This version introduces JsonIgnoreCondition enum:
/// When specified on JsonSerializerOptions.DefaultIgnoreCondition, /// determines when properties and fields across the type graph are ignored. /// When specified on JsonIgnoreAttribute.Condition, controls whether /// a property is ignored during serialization and deserialization. This option /// overrides the setting on JsonSerializerOptions.DefaultIgnoreCondition. public enum JsonIgnoreCondition { /// Property is never ignored during serialization or deserialization. Never = 0, /// Property is always ignored during serialization and deserialization. Always = 1, /// If the value is the default, the property is ignored during serialization. /// This is applied to both reference and value-type properties and fields. WhenWritingDefault = 2, /// If the value is <see langword="null"/>, the property is ignored during serialization. /// This is applied only to reference-type properties and fields. WhenWritingNull = 3, }
具体地 JsonIgnoreCondition.WhenWritingDefault 将取消false布尔值.可以以两种方式之一来应用它.首先,您可以使用 JsonIgnoreAttribute.Condition :
Specifically JsonIgnoreCondition.WhenWritingDefault will suppress false Boolean values. It may be applied in one of two ways. Firstly, you can apply it directly to a member using JsonIgnoreAttribute.Condition:
public class Model { [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool Value { get; set; } }演示小提琴#1 此处.
其次,您可以在 JsonSerializerOptions.DefaultIgnoreCondition :
Secondly, you may set it in JsonSerializerOptions.DefaultIgnoreCondition:
指定一个条件,以确定在序列化或反序列化期间何时忽略具有默认值的属性.默认值为从不.
即给出以下模型:
public class Model { public bool Value { get; set; } }您可以如下进行序列化,以在运行时跳过false值的序列化:
You may serialize it as follows to skip serialization of false values in runtime:
var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; var json = JsonSerializer.Serialize(model, options);演示小提琴#2 此处.
注意:
JsonIgnoreCondition 的noreferrer>.Net 5.0文档似乎存在一些错误.首先,他们声称WhenWritingDefault意味着
只有在null时属性才会被忽略.
但是,实际上,如 default ,则该属性将被忽略. 0.0-rtm.20519.4/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonIgnoreCondition.cs"rel =" nofollow noreferrer>源代码.
其次,他们声称WhenWritingNull
仅适用于引用类型的属性和字段.
is applied only to reference-type properties and fields.
但是,测试表明它适用于可空值类型成员.例如.给定模型:
However, testing shows that it applies to nullable value type members as well. E.g. given the model:
public class Model { public bool? Value { get; set; } }在Value为空的情况下,使用DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault序列化将省略Value:
Serializing with DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault omits Value when it it is null:
var model = new Model(); // Leave value null var options = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault }; Console.WriteLine(JsonSerializer.Serialize(model, options)); // Prints {}演示小提琴#3 此处.
设置JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault不仅适用于bool,还适用于所有值类型.因此,如果您有任何double,int,DateTime,decimal(包括具有指定位数的零值小数,例如decimal.Parse("0.0000"))或其他值类型成员,则当它们该值等于默认值.
Setting JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault applies to all value types not just bool. Thus if you have any double, int, DateTime, decimal (including a zero-valued decimal with a specified number of digits such as decimal.Parse("0.0000")) or other value type members, they will be omitted when the value equals the default.
似乎没有一种方法可以全局跳过仅默认bool值成员的序列化,同时仍然序列化其他值类型的成员,这可以在Json.NET中通过自定义合同解析器完成.
There does not appear to be a way to globally skip serialization of only default bool valued members while still serializing other value-type members, which could be done in Json.NET via a custom contract resolver.
演示小提琴#4 此处.
在确定成员是否具有默认值时,使用值的类型(即default(T))的默认值.与 Json.NET 不同, DefaultValueAttribute .
In determining whether a member has a default value, the default value for the type of the value (i.e. default(T)) is used. Unlike Json.NET, DefaultValueAttribute is not taken into consideration.
演示小提琴#5 此处.
.Net 5.0之前,您需要创建一个Model)自定义JsonConverter (如何操作)rel ="nofollow noreferrer">自定义JsonConverter ,并根据需要手动跳过或序列化每个成员.
Prior to .Net 5.0 you would need to create a custom JsonConverter for the containing type (i.e. Model in the above examples) and manually skip or serialize each member as desired.
更多推荐
如何使用System.Text.Json忽略错误值
发布评论