-
Notifications
You must be signed in to change notification settings - Fork 409
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
[Feature Request] Add an override of TryGetClaimValue with a JsonTypeInfo parameter #2255
Comments
Note: this scenario is already possible/supported using something like this: if (token.TryGetPayloadValue("claim", out JsonElement element) &&
element.Deserialize<ClrType>(jsonTypeInfo))
{
// ...
} |
@kevinchalet we currently only keep the JsonElement when we hit a JsonArray or JsonObject, all others are deserialized into C# types. |
@keegan-caruso if this was available when the token was being deserialized the type would be created at read time and the cpu cycles used to create the JsonElement then back to the type would be avoided. |
Wait, really, @brentschmaltz? That sounds like a weird design to me. Any particular reason why not everything can be represented as a |
@kevinchalet There are performance reasons why we don't want to store everything as a JsonElement. We could easily have a switch where we could store the JsonElement. |
I had not realized you were materializing all the claims as strongly-typed CLR objects upfront: when we mentioned Now, this raises a question regarding the API suggested by @jmprieur: won't you end up deserializing claims twice with this API? (once by |
@kevinchalet when we are processing the json (base64urldecoding, reading, etc...) we are working in an ArrayPool buffer and we want to get as much work done as possible. The reason for the TryGetClaimValue with JsonTypeInfo is so that a user could obtain a complicated custom type. We only support a limited number of types: Collection, Dictionary<string, string> etc... |
Is your feature request related to a problem? Please describe.
Developers might want to deserialize claims in ways we can't anticipate, and are ready to direct the .NET 7/8 compiler to generate the code for the serializers/deserializers.
Describe the solution you'd like
Have an override of JsonWebToken.TryGetClaim that takes a JsonTypeInfo parameter in addition to the name of the claim and the out parameter
Describe alternatives you've considered
Trying to guess all the types that need to be deserialized
Additional context
The only way, today, to make JsonSerializer work with trimming/AOT is to use the JSON source generator - which produces these JsonTypeInfo classes. Then when you need to serialize/deserialize to/from objects, you pass the correct JsonTypeInfo you got from the source generator into the API.
Any call to JsonSerializer that doesn't pass the JsonTypeInfo will result in trimming and AOT warnings (and probably will fail at runtime when you AOT your app)
Resources:
The text was updated successfully, but these errors were encountered: