Skip to content

JsonIgnoreCondition.WhenWritingDefault and double #41630

Closed

Description

@steveoh

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

Activity

  1. added
    untriaged
    on Aug 31, 2020
  2. pinkfloydx33 commented on Sep 1, 2020

    @pinkfloydx33

    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.

  3. steveoh commented on Sep 1, 2020

    @steveoh
    Author

    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?

  4. pinkfloydx33 commented on Sep 1, 2020

    @pinkfloydx33

    I believe you want JsonIgnoreCondition.Always

  5. layomia commented on Sep 1, 2020

    @layomia
    Contributor

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

  6. removed
    untriaged
    on Sep 1, 2020
  7. added this to the 5.0.0 milestone on Sep 1, 2020
  8. pinkfloydx33 commented on Sep 1, 2020

    @pinkfloydx33

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

  9. layomia commented on Sep 2, 2020

    @layomia
    Contributor

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

  10. ghost locked as resolved and limited conversation to collaborators on Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions