You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonIgnoreCondition.WhenWritingDefault and double #41630
When the prop is set to -1, which is the default value, the property is serialized when JsonIgnoreCondition.WhenWritingDefault is set. I would expect that property to not be serialized.
Aside: your use of & in JsonIgnoreCondition.WhenWritingNull & JsonIgnoreCondition.WhenWritingDefault is a little weird. That equates to 3&2==2 which equals WhenWritingDefault. Assuming you meant to use | I wanted to point out that it's not a Flags enum; it can only ever be one or the other.
Ok thank you for clearing up that misconception. I will need to use more nullable types then I suspect to get a similar behavior.
How do I get both behaviors of ignoring nulls and defaults? If I set ignorenullvalues I am greeted with System.InvalidOperationException: 'IgnoreNullValues' and 'DefaultIgnoreCondition' cannot both be set to non-default values. Do I need to write my own converters?
@steveoh@pinkfloydx33 , the setting you want is JsonIgnoreCondition.WhenWritingDefault. This will ignore both null for nullable types and default(T) for non-nullable types. JsonIgnoreCondition.WhenWritingNull will ignore only null properties. Please let us know if you still face issues with this.
wrt. using DefaultValueAttribute or a similar mechanism to specify a custom default value, that is being tracked by #36236. Will close this issue as a duplicate.
@pinkfloydx33 - JsonIgnoreCondition.Always means always ignore when serializing or deserializing. This is the default value of JsonIgnoreAttribute.Condition, but it is not a valid value for JsonSerializerOptions.DefaultIgnoreCondition - it doesn't make sense to ignore all properties for both deserialization and serialization across the entire object graph.
Activity
pinkfloydx33 commentedon Sep 1, 2020
The serializer only looks at the actual default value of the type, meaning
default(T). It doesn't look at theDefaultValueAttributeruntime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs
Lines 137 to 138 in 756addd
Aside: your use of
&inJsonIgnoreCondition.WhenWritingNull & JsonIgnoreCondition.WhenWritingDefaultis a little weird. That equates to3&2==2which equalsWhenWritingDefault. Assuming you meant to use|I wanted to point out that it's not aFlagsenum; it can only ever be one or the other.steveoh commentedon Sep 1, 2020
Ok thank you for clearing up that misconception. I will need to use more nullable types then I suspect to get a similar behavior.
How do I get both behaviors of ignoring nulls and defaults? If I set ignorenullvalues I am greeted with
System.InvalidOperationException: 'IgnoreNullValues' and 'DefaultIgnoreCondition' cannot both be set to non-default values.Do I need to write my own converters?pinkfloydx33 commentedon Sep 1, 2020
I believe you want
JsonIgnoreCondition.Alwayslayomia commentedon Sep 1, 2020
@steveoh @pinkfloydx33 , the setting you want is
JsonIgnoreCondition.WhenWritingDefault. This will ignore bothnullfor nullable types anddefault(T)for non-nullable types.JsonIgnoreCondition.WhenWritingNullwill ignore onlynullproperties. Please let us know if you still face issues with this.wrt. using
DefaultValueAttributeor a similar mechanism to specify a custom default value, that is being tracked by #36236. Will close this issue as a duplicate.pinkfloydx33 commentedon Sep 1, 2020
@layomia what is the behavior for
Always? I'd expect that to behave how you describeWhenWritingDefault. Or is that a Serialize vs. Deserialize thing?layomia commentedon Sep 2, 2020
@pinkfloydx33 -
JsonIgnoreCondition.Alwaysmeans always ignore when serializing or deserializing. This is the default value ofJsonIgnoreAttribute.Condition, but it is not a valid value forJsonSerializerOptions.DefaultIgnoreCondition- it doesn't make sense to ignore all properties for both deserialization and serialization across the entire object graph.We'll add documentation for all of this behavior - https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-how-to.