-
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
JsonSerializer uses different default values for properties between reading and writing, causing data loss in round-trips #110423
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
I would consider this by-design behaviour, and would be a breaking change assuming we tried to change it. You can either remove the optional parameter from the constructor, use a secondary deserialization constructor like you suggest, or avoid skipping default values. |
@eiriktsarpalis, a fix can always be opt-in and thus non-breaking (e.g. by adding |
I would think so, yes. It is called an ignore condition which means that it only controls the write side of things. Correspondingly, specifying a deserialization constructor only controls deserialization aspects and has no impact on serialization. In isolation I would regard either setting to be sound even if they can conflict in the way you're describing. The serializer doesn't guarantee that any type can be roundtripped for every possible configuration -- it will make a best effort to ensure this but beyond that it becomes the responsibility of the user to model something that works. |
Description
Serialization uses the default value of the type (e.g. 0 for int), while deserialization uses the default value of the parameter in the constructor.
Reproduction Steps
Expected behavior
Either of these two would be acceptable:
or:
Actual behavior
Regression?
No response
Known Workarounds
Add another constructor that does not have default parameter values, and mark that with
[JsonConstructor]
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: