Skip to content

System.Text.Json: IgnoreNullValues ignores field that have a default value #38800

Closed

Description

@molinch

Description

When JsonSerializerOptions IgnoreNullValues is true then it won't serialize value type fields having default value.
See below test case as an example:

        [Fact]
        public void Should_Not_Be_An_Empty_Json_When_IgnoreNullValues_Is_True()
        {
            var options = new System.Text.Json.JsonSerializerOptions()
            {
                IgnoreNullValues = true
            };
            var emptyObj = new { };
            var obj = new
            {
                PlayOrder = 0
            };

            var jsonForEmptyObj = System.Text.Json.JsonSerializer.Serialize(emptyObj, options);
            var json = System.Text.Json.JsonSerializer.Serialize(obj, options);

            Assert.NotEqual(jsonForEmptyObj, json);
        }

Configuration

.NET SDK: 5.0.100-preview.6.20318.15
Windows 10 or Linux image mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim show the same behaviour
Architecture: x64, but doesn't seem specific to it

Regression?

It is a regression in .NET Core 3.1 it was working as expected.
It was also fine in former .NET 5 versions.

Other information

I would be happy to help adding the test case to the framework tests if guided.

Activity

  1. added
    untriaged
    on Jul 6, 2020
  2. molinch commented on Jul 6, 2020

    @molinch
    Author

    This commit might be the root cause: c2291eb#diff-6945dba66187c255555ac8232ef4584dR140

    See how it added that with Options.IgnoreNullValues we ignore the default values on read/write.
    While that is correct for reference types, it's not the case for value types.

    From that commit I see that IgnoreNullValues has been obsoleted, replaced by DefaultIgnoreCondition.
    Still I believe that:

    • existing code should still work as it used to
    • we still need to differentiate serialization of null from default value, these have very different semantic in Javascript (which is the target of that serializer)

    From the mentioned commit:
    image
    I think that WhenNull should be kept as an option, which would have the same effect as obsolete Options.IgnoreNullValues.
    Most likely most folks will want that behaviour.

  3. added a commit that references this issue on Jul 6, 2020
  4. changed the title [-]System.Text.Json: IgnoreNullValues ignores field that are zero[/-] [+]System.Text.Json: IgnoreNullValues ignores field that have a default value[/+] on Jul 6, 2020
  5. removed
    untriaged
    on Jul 6, 2020
  6. added this to the 5.0.0 milestone on Jul 6, 2020
  7. self-assigned this
    on Jul 6, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions