Skip to content

System.Text.Json Desrialization to an IParsable<T> #86529

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

Closed
SDP190 opened this issue May 19, 2023 · 2 comments
Closed

System.Text.Json Desrialization to an IParsable<T> #86529

SDP190 opened this issue May 19, 2023 · 2 comments

Comments

@SDP190
Copy link

SDP190 commented May 19, 2023

Can we make Json deserialization to make use of IParsable types, so for example if a Json property has a string value, but the converting-to type is not a string but is a complex type which implements IParsable<T>, then the de-serialization process should automatically call IParsable.Parse() to try and create an instance of that type.

We may want to restrict it to only allow parsing of value types (structs) ?

Or we may want to have a special attribute on top of IParsable type properties to define whether deserialization can use IParsable.Parse for that specific property or not, something like:
[JsonAllowIParsableDeserialization]
if the attribute is applied we can adhere to it, if not we may have a default decision whether yes or not to use IParsable<T>.Parse (which itself might in turn depend if its a value type or object).

Besides above mentioned attribute, we may want to have an general option in the SerializationOptions passed to the Deserialize method.

Is this already the behavior of Json deserialization now?, as far as I have searched its not documented anywhere.

@ghost ghost added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels May 19, 2023
@ghost
Copy link

ghost commented May 19, 2023

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

can we support implementing Json Deserialization to make use of IParsable types, so for example if a json property has a string value, but the converting-to type is not a string but is implementing IParsable, then de-serialization should use IParsable.Parse to try create an instance of that type.

Is this already the behavior of Json deserialization now?, as far as I have searched its not documented anywhere.

Author: SDP190
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

Related to #84305. Given the nature of STJ supporting the UTF-8 variants of the parser/formatter interfaces probably makes more sense.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label May 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jun 21, 2023
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

2 participants