-
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
System.Text.Json should support deserializing into mutable collections exposed via readonly members #38705
Comments
I would think this behavior would be by design. A property defined as " |
Yes it would be a read-only property then. But guess what, IgnoreReadOnlyProperties is set to false. So I would think it would include read-only properties then, right? Might be a bug with private setter or with readonly properties. Both are now supported but in my example it just don't work. FYI: In Newtonsoft JSON it works out of the box. Just saying in case you would think it would be technically impossible, it is not |
It doesn't, according to this: JamesNK/Newtonsoft.Json#703. |
If you would have taken my example and ran it with Newtonsoft you would have noticed it does
Also this side note does not change my actual issue about both readonly and private setters are officially supported but practically it doesn't work. So let's keep focusing on System.Text.Json instead of Newtonsoft for now. |
I see - you don't want to be able to actually set readonly properties, but to populate existing collections with deserialized data. (I assume Newtonsoft doesn't actually set the readonly property in your sample, but clears the collection and adds new items to it to sidestep problems discussed at length in the Newtonsoft issue.) |
Yes exactly! 👍 I want the property to be a new collection with all the data from the deserialization (and if there is no data, it leaves the default behavior) I'm sorry, if I was misleading about this. |
I've updated the issue title to more accurately reflect what's being requested. |
This is a duplicate of #30258. |
Description
The pull request #34675 added support for private setters. And the default value for
IgnoreReadOnlyProperties
isfalse
. So I would have assumed something like:[JsonInclude] public ObservableCollection<int> MyInt { get; } = new ObservableCollection<int>();
would work but it does not. It still requires theprivate set;
to explicit written.Configuration
Other information
Output:
The text was updated successfully, but these errors were encountered: