-
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
Questions about JsonSerializer.Deserialize #83704
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsI'm having a problem where I have a record base type (not abstract and interface) and I use Because the service program belongs to someone else's, I am worried that he will introduce unknown types, so I hope to accept it so that I can add new types so that I can better analyze the data. Also, I wonder if at
|
Last I can recall, you cannot have am actual property for the discriminator field. There's definitely some older issues in this repo requesting that it be supported, so it's quite possible it's changed by now, but I don't remember seeing any updates just yet. Essentially what I'm saying is: remove the property. If you really want/need it, one solution is to make it an abstract property that you decorate with |
it's currently not supported. My suggestion will be to directly implement static abstract member with the same type discriminators. There is currently couple of parallel issues with treating metadata similar to properties:
possibly we should expose polymorphic metadata as regular JsonPropertyInfo to allow these scenarios to work. |
Marking this as Future because we will unlikely pick this up in 8.0 timeline |
In order to move this forward sooner it would be good if we created specific design proposal on how to modernize metadata handling (i.e. show them as JsonPropertyInfo, then perhaps trying to prototype that and seeing if that is sound plan). Once we have specific plan we all agree on we can consider that for vNext or we can leave it for volounteers to pick up. |
This is by design -- type discriminators are identifiers associated with particular derived types on the type level and not values that can be read from or written to object properties. Such a feature would not be polymorphism per se, rather, it is informing the contract of a serialized type dynamically based on contents of its values (and not its runtime type). As such, it is not in scope for the polymorphism feature. |
请不要拿 直接捕捉异常,当派生类拥有某个属性中存在其他的多态类型,这样我只能将整段原始数据进行分析,很不方便,因为他无法将原始类型与原始数据写入。 获取原始数据:考虑到JSON解析是使用 微软想让人使用 Please don't use 'This is by design' as an excuse. It's flawed. Directly catching the exception, when the derived class has a property with other polymorphic types, then I can only analyze the whole original data, which is inconvenient, because he can't write the original type and original data. Get the raw data: considering JSON parsing is using Microsoft wants people to use |
I'm sorry you feel that way, but as already mentioned, the type discriminator is designed to be a type-level mapping and does not bind to runtime values of properties (either in serialization or deserialization). As with any feature of such a nature, it is necessarily opinionated so it can't cater to every use case (similar to how the |
I'm having a problem where I have a record base type (not abstract and interface) and I use
JsonPolymorphic
, puttingTypeDiscriminatorPropertyName = "type"
, andIgnoreUnrecognizedTypeDiscriminators = true
when an unknown type is encountered, He will return the base type, but in fact it does not assign a value to the base type "type", is this a bug?Because the service program belongs to someone else's, I am worried that he will introduce unknown types, so I hope to accept it so that I can add new types so that I can better analyze the data.
Also, I wonder if at
JsonSerializer.Deserialize
can bind unresolved content data to custom properties likeJsonPolymorphic(TypeDiscriminatorPropertyName = "type")
, so that the data can be analyzedThe text was updated successfully, but these errors were encountered: