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

System.Text.Json -> Deserialize Null to Integer Property #74008

Closed
jogibear9988 opened this issue Aug 16, 2022 · 5 comments
Closed

System.Text.Json -> Deserialize Null to Integer Property #74008

jogibear9988 opened this issue Aug 16, 2022 · 5 comments

Comments

@jogibear9988
Copy link

Description

We hav the following Code:

    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public virtual int OrderIndex { get; set; }

But it throws a exception when we deserialize a "null" value to a Object with this et

Reproduction Steps

see code above

Expected behavior

should use default value for int

Actual behavior

does not work

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Aug 16, 2022
@ghost
Copy link

ghost commented Aug 16, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

We hav the following Code:

    [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
    public virtual int OrderIndex { get; set; }

But it throws a exception when we deserialize a "null" value to a Object with this et

Reproduction Steps

see code above

Expected behavior

should use default value for int

Actual behavior

does not work

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: jogibear9988
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@layomia
Copy link
Contributor

layomia commented Aug 18, 2022

JsonIgnoreCondition.WhenWritingNull is only applied to writing/serialization, not deserialization. There's a proposal to extend the enum to include something like JsonIgnoreCondition.WhenReadingNull - #66490. I'll close this as part of that larger issue.

The custom serialization contracts features provides a predicate JsonPropertyInfo.ShouldSerialize to determine whether a property's value should be serialized. I believe the same was considered for deserialization. It would be helpful for scenarios like this. cc @krwq, @eiriktsarpalis.

@layomia layomia closed this as completed Aug 18, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 18, 2022
@krwq
Copy link
Member

krwq commented Aug 18, 2022

could you simply customize JsonPropertyInfo so that Set ignores null value and does custom behavior?

@jogibear9988
Copy link
Author

could you simply customize JsonPropertyInfo so that Set ignores null value and does custom behavior?

we don't use the source code generators at the moment.

@krwq
Copy link
Member

krwq commented Aug 19, 2022

@jogibear9988 that's a contract customization feature we're shipping in 7.0 it works with both code-gen and reflection, it should be in pre-release nuget package 7.0.0-preview.6 or higher

JsonSerializerOptions options =...;
options.TypeInfoResolver = new DefaultJsonTypeInfoResolver()
{
  Modifiers = { typeInfo => { if (typeInfo.Type == typeof(...)) { /* typeInfo.Properties modification here */ } } }
};

// then: JsonSerializer.Deserialize<...>(json, options);

@ghost ghost locked as resolved and limited conversation to collaborators Sep 18, 2022
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