-
Notifications
You must be signed in to change notification settings - Fork 33
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
Add function overloads with Type as parameter for KiotaSerializer.Deserialize<T> #210
Comments
Hi @dansmitt |
Hi @baywet // from Microsoft.Kiota.Abstractions.dll (1.7.5)
public static T? Deserialize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] T>(string serializedRepresentation) where T : IParsable``` Note that the generic Type T has to be explicitly defined here as it can't be implicitly derived from the methods parameters. Hence if there would be a function where the type can be parsed as a parameter: public static IParsable? Deserialize(string serializedRepresentation, Type targetTypeToSerializeTo) we would not have to use reflection to set the generic type I'm not sure if there is a more specific type then IParsable - that this function could return - but that would already help a lot. For reference System.Text.Json has also a function like that in their Serializer reperteure of // from System.Text.Json.dll (8.0.3)
public static object? Deserialize([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, Type returnType, JsonSerializerOptions? options = null) |
@baywet what @MichaMican says |
Thanks for the detailed explanation here. So assuming a bit of refactoring, it should be easy to implement an overload that accepts the type directly. Is this something you'd be interested in submitting a pull request for? |
|
We (@MichaMican and I) have implemented an TextFormatter extension to use Kiota as thge standard serializer for
IParsable
objects when working with ASP.NET Controllers.See: microsoftgraph/msgraph-sdk-dotnet#2343 (comment)
In our TextInputFormatter we have to fallback to using Reflection because we work with an implementation of IParsable object that we have to set as the generic type of
KiotaSerializer.Deserialize<T>
during run time. As of now the function unfortunately has no overload to specify the Type as a parameter which would be super usefull in our case as we could then imrpove code readability by not using reflection.Snippet from the TextInputFormatter:
The text was updated successfully, but these errors were encountered: