Skip to content
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

Using this with clientside blazor #32

Closed
americanslon opened this issue Mar 12, 2020 · 5 comments
Closed

Using this with clientside blazor #32

americanslon opened this issue Mar 12, 2020 · 5 comments
Assignees
Labels
question Further information is requested

Comments

@americanslon
Copy link

Do you know how this can be setup in the client side blazor to deserialized based on the descriminator.

The serialization on the net.core web api side works great, _t gets added but the blazor side is not aware of _t and doesn't deserialize into derived types.

@mcatanzariti
Copy link
Member

mcatanzariti commented Mar 13, 2020

@americanslon Are you referring to this technologie?
https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor

Could you provide the client sample code that fails, please?

@americanslon
Copy link
Author

Yep that's the one.
Nothing fails, I don't even understand how to bootstrap/initialize it with blazor.
Judging by your question you probably don't know either. It might be something worthwhile to look at to make sure this library works with it

@mcatanzariti
Copy link
Member

Please let me help you by providing a code sample!

@americanslon
Copy link
Author

There is nothing to provide....I don't understand how to set this up.
For example there are not controllers in blazor so there is no JsonSerializerOptions that I can find too attach Dahomey.Json to. Nothing to call SetupExtensions on.

@mcatanzariti
Copy link
Member

What I am expecting is the code where you call GetJsonAsync or something similar to be sure I'm looking at the right API

Anyway, by digging into this technology, I noticed that there is no way to provide a JsonSerializerOptions instance to their HttpClient extension.

You need to call Dahomey.Json extension method SetupExtension on JsonSerializerOptions in order to let polymorphism work.

the issue corresponding to this problem is here:
dotnet/aspnetcore#19368

and the task to implement a better HttpClientExtensions is
dotnet/runtime#32937

However, you could implement it yourself, starting from
https://github.com/dotnet/aspnetcore/blob/master/src/Components/Blazor/Http/src/HttpClientJsonExtensions.cs

for instance

public static async Task<T> GetJsonAsync<T>(this HttpClient httpClient, string requestUri)
{
    var stringContent = await httpClient.GetStringAsync(requestUri);
    return JsonSerializer.Deserialize<T>(stringContent, JsonSerializerOptionsProvider.Options);
}

would be modified into

public static async Task<T> GetJsonAsync<T>(this HttpClient httpClient, string requestUri, JsonSerializerOptions options)
{
    var stringContent = await httpClient.GetStringAsync(requestUri);
    return JsonSerializer.Deserialize<T>(stringContent, options);
}

Hope it helps

@mcatanzariti mcatanzariti self-assigned this Mar 13, 2020
@mcatanzariti mcatanzariti added the question Further information is requested label Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants