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

JsonSerializerOptions.IgnoreNullValues=true causes JsonConverter override to never be called #33369

Closed
pasisavolainen opened this issue Mar 9, 2020 · 2 comments

Comments

@pasisavolainen
Copy link

pasisavolainen commented Mar 9, 2020

Tried to override null-ignore behavior on some individual cases where it makes sense, but no such luck.

My reasonable expectation is that if a property has converter specified (and converter gets the converter options), then the converter would be called on all and any values that the property is. However the IgnoreNullValue = true causes the converter to never be called and to be unable to make the decision in application.

example:

    internal class KeepNullsConverterExample : JsonConverter<string>
    {
        public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
            => throw new NotImplementedException();

        public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
            => writer.WriteStringValue(value.ToString());
    }
    class TestObject
    {
        [JsonConverter(typeof(KeepNullsConverterExample))]
        public string Foo { get; set; }
    }
    public class OptionItemTests
    {
        [Fact]
        public void NullValueSurvives()
        {
            var items = new TestObject[]
            {
                new TestObject{ Foo = "a" },
                new TestObject{ Foo = null},
            };

            var jsonOptions = new JsonSerializerOptions
            {
                IgnoreNullValues = true,
            };

            var json = JsonSerializer.Serialize(items, jsonOptions);

            var expected = "[{\"Foo\":\"a\"},{\"Foo\":null}]";

            Assert.Equal(expected, json);
        }
    }
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Mar 9, 2020
@layomia
Copy link
Contributor

layomia commented Mar 11, 2020

We currently don't pass null to user converters, so even if IgnoreNullValues was false in your scenario, the converter's write method would still not be called. For 5.0, we are likely to add a bool property on JsonConverter (as part of work in #1562), say HandleNullValues (defaults to false) for converter's to opt in to handle null tokens. cc @steveharter.

New semantics need to be added for a property not to be ignored when IgnoreNullValues is true. This seems like a good candidate for the Never condition on the new JsonIgnoreCondition property being proposed in #30687. Your class definition could look like this:

class TestObject
{
    [JsonConverter(typeof(KeepNullsConverterExample))]
    [JsonIgnore(Condition = JsonIgnoreCondition.Never)]
    public string Foo { get; set; }
}

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Mar 11, 2020
@layomia layomia added this to the 5.0 milestone Mar 11, 2020
@layomia
Copy link
Contributor

layomia commented Apr 4, 2020

Closing as duplicate - #34439 tracks providing an opt-in for converters to handle null, and properties can be excluded from being ignored using JsonIgnoreCondition.Never introduced in #30687.

@layomia layomia closed this as completed Apr 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 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

3 participants