-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
IgnoreNullValues does not work with non-nullable value types #30795
Comments
Here's a sample repro (looks like it can repro with any value type - not just DateTime, like int32, but works fine with reference types like string): [Fact]
public static void IgnoreNullValuesBug()
{
var options = new JsonSerializerOptions { IgnoreNullValues = true };
Temp result = JsonSerializer.Deserialize<Temp>("{\"Foo\": null}", options);
Console.WriteLine(JsonSerializer.Serialize(result, options));
}
public class Temp
{
public DateTime Foo { get; set; }
}
Note that serialization (i.e. writing) works as expected (where null values are getting ignored). The issue is specific to deserialization (reading). |
Work-arounds:
|
There is a question here on correctness and validation. Since the value can never be null anyway, should I compared against Json.NET (using |
Some options assuming
|
@rynowak, @JamesNK - what do you think? To me, option 3 makes the most sense. When I see the Although, I can see an argument to be made about option 1, but that requires the user to change their object model or workaround it with their own converter (maybe that's OK). Given this is effectively by-design, moving to 5.0 for now. |
For the "ignore null" feature we discussed the following benefits:
We never discussed whether this feature can be used to disable validation by removing null values from non-nullable structs. |
Option 3 makes the most sense. I don't think there's anything terrible about the JSON.NET behaviour. |
Punting this to future, as we don't want to loosen this behavior before designing upcoming validation features including required properties and (de)serialization callbacks. |
Today I ran into an issue with this with the a
When the body looks like this:
And the method looks like this:
The |
Would it make sense to try to fix an obsolete setting? If not, we could possibly evaluate extending We might instead want to consider a more extensible model for ignoring properties: #55781 |
In API review we determined that there was no good scenario for ignoring |
Rather than extending functionality of an obsoleted property, we should look to providing more extensibility points so that users can have a greater deal of control over what properties should be ignored and when. This was expressed initially in #55781 but ultimately we'd be looking at addressing this via #63686. |
Setting JsonSerializerOptions property of IgnoreNullValues to true does not work.
When attempting to deserialize a DateTime value that is null in the json string it throws the exception below. I thought setting the option to IgnoreNullValues should ignore the property, no?
converted to System.DateTime. Path: $.value[0].deletedDateTime
InvalidOperationException: Cannot get the value of a token type 'Null' as a string.
Thanks
Youcef
The text was updated successfully, but these errors were encountered: