Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonIgnoreCondition.WhenWritingDefault and double #41630

Closed
steveoh opened this issue Aug 31, 2020 · 6 comments
Closed

JsonIgnoreCondition.WhenWritingDefault and double #41630

steveoh opened this issue Aug 31, 2020 · 6 comments

Comments

@steveoh
Copy link

steveoh commented Aug 31, 2020

Description

// startup
.AddJsonOptions(options => {
  options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
  options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull & JsonIgnoreCondition.WhenWritingDefault;
  options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});

// ...

[DefaultValue(typeof(decimal), "-1")]
[DefaultValue(-1.0)]
[DefaultValue(-1D)]
public double Prop { 
	get => _prop;
    set => _prop = Math.Round(value, 2);
}

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.

Configuration

osx catalina x64
5.0.100-preview.8.20417.9

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Aug 31, 2020
@pinkfloydx33
Copy link

pinkfloydx33 commented Sep 1, 2020

The serializer only looks at the actual default value of the type, meaning default(T). It doesn't look at the DefaultValueAttribute

if (IgnoreDefaultValuesOnWrite && (
default(T) == null ? value == null : EqualityComparer<T>.Default.Equals(default, value)))

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.

@steveoh
Copy link
Author

steveoh commented 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
Copy link

I believe you want JsonIgnoreCondition.Always

@layomia
Copy link
Contributor

layomia commented Sep 1, 2020

@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.

@layomia layomia closed this as completed Sep 1, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Sep 1, 2020
@layomia layomia added this to the 5.0.0 milestone Sep 1, 2020
@pinkfloydx33
Copy link

@layomia what is the behavior for Always? I'd expect that to behave how you describe WhenWritingDefault. Or is that a Serialize vs. Deserialize thing?

@layomia
Copy link
Contributor

layomia commented Sep 2, 2020

@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.

We'll add documentation for all of this behavior - https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-how-to.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants