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

[Blazor Webassembly] Give JsonSerializerOptions to HttpClient Make result "empty" #21322

Closed
julienGrd opened this issue Apr 29, 2020 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@julienGrd
Copy link

Hello Guys,

It seem the new System.Text.Json Api can now handle ReferenceLoopHandling as suggest this issue (#21187 (comment)).

So i try implement this in my app to replace Newtonsoft.Json.

I precise i have this version of System.Net.Http.Json : 5.0.0-preview.3.20214.6

I configure my server project to activate this

pServices.AddMvc()
            .AddJsonOptions(options =>
             {
                 options.JsonSerializerOptions.ReferenceHandling = ReferenceHandling.Preserve;
             });

And my client project

JsonSerializerOptions options = new JsonSerializerOptions()
        {
            ReferenceHandling = ReferenceHandling.Preserve
        };

var result = await sender.GetFromJsonAsync<T>(requestUrl, options);

I don't have errors, i retrieve an array with the correct amount of items, but all the properties are empty.

I notice is the same thing with an empty JsonSerializerOptions, so it seem not related to this specific option or to the server project.

To reproduce in the default project, just add an empty option :

forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast", new System.Text.Json.JsonSerializerOptions());
//all the forecast here have theirs properties empty

Thanks !

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly labels Apr 29, 2020
@javiercn
Copy link
Member

@julienGrd thanks for contacting us.

I don't think this is Blazor webassembly now since GetFromJsonAsync is coming from the new JSON extensions for HttpClient. That said, can you post more details about your model, make sure you have linking turned off (to ensure the linker is not removing things an causing the serialization to fail)

@pranavkm do you have any thoughts on this?

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Apr 29, 2020
@pranavkm
Copy link
Contributor

MVC 3.x's output formatter does not support features introduced in System.Text.Json 5.0. This includes the ReferenceHandling property. You're also missing the casing behavior configured by default by GetFromJsonAsync:

new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true,
    ReferenceHandling = ReferenceHandling.Preserve,
}

@pranavkm pranavkm added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Apr 29, 2020
@ghost ghost added the Status: Resolved label Apr 29, 2020
@julienGrd
Copy link
Author

MVC 3.x's output formatter does not support features introduced in System.Text.Json 5.0. This includes the ReferenceHandling property. You're also missing the casing behavior configured by default by GetFromJsonAsync:

new JsonSerializerOptions()
{
    PropertyNameCaseInsensitive = true,
    ReferenceHandling = ReferenceHandling.Preserve,
}

It work by adding the PropertyNameCaseInsensitive property, thanks !

What do you mean by "MVC 3.x's output formatter does not support features introduced in System.Text.Json 5.0" ?

Im actually in .net 5 preview in my server, so its OK for me ?

There is any way to achieve this (use System.Text.Json completely) or im stuck with NewtonSoft ?

Other question : is it possible to use both system on server ? For example use System.Text.Json generally but on some method/controller use newtonsoft ?

@pranavkm
Copy link
Contributor

What do you mean by "MVC 3.x's output formatter does not support features introduced in System.Text.Json 5.0" ?
dotnet/runtime#30445 has some more context. MVC's output formatter currently only supports configuring the subset of features supported of S.T.Json 3.x. This should be resolved as part of the preview4 release.

: is it possible to use both system on server ? For example use System.Text.Json generally but on some method/controller use newtonsoft ?
You can tell MVC what set of formatters to use to format an object:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.objectresult.formatters?view=aspnetcore-3.1#Microsoft_AspNetCore_Mvc_ObjectResult_Formatters

@ghost
Copy link

ghost commented May 1, 2020

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed May 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators May 31, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components feature-blazor-wasm This issue is related to and / or impacts Blazor WebAssembly ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants