dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

Trying to convert JSON to Object using Newtonsoft freezes #16001

Closed tessierp closed 4 years ago

tessierp commented 6 years ago

Hi,

I've been trying to use Newtonsoft to convert a json string to an my expected model but each time I include Newtonsoft in a view and use it, it freezes. For example lets say I have a view with the following : ` @functions {

IEnumerable<RegionDTO> _regions;

protected override async Task OnInitAsync()
{
    Http.BaseAddress = new System.Uri("http://localhost:10000");
     regionData = Http.GetStringAsync("/api/Region");
     var parsedData = JObject.Parse(regionDataTask.Result);
     _regions = parsedData.ToObject<IEnumerable<RegionDTO>>();
}

class RegionDTO
{
    Guid RegionId { get; set; }
    string RegionName { get; set; }
}

} ` I will get no output, nothing will happen. I tested this code including JsonConvert.DeserializeObject from Newtonsoft and the same thing happens.

I tested this outside of Blazor and it works fine.

Now you may wonder, why would I be doing this if I can call GetJsonAsync directly from Http? Well, initially the API I coded didn't have

services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });

so all the json data started with a lowercase and it wouldn't resolve to my models which have properties starting with a Upper case for the first letter (this is probably a problem wit the HttpClient).

Anyways, anyways else has this problem with Blazor using NewtonSoft?

danroth27 commented 6 years ago

Yes, there is a known issue with using Json.NET with Blazor right now that we think will be addressed once we update to the latest mono.wasm support for our 0.4.0 release (#831).

Also, we have done work for 0.4.0 that will make the default JSON handling in Blazor be more flexible with casing on deserialization.

Daddoon commented 6 years ago

@tessierp You may take a try to my Json.NET.Aot package.

The only drawback, i found that it is slow when used with asmjs on low end devices. On WebAssembly it run smoothly IMO.

danroth27 commented 6 years ago

Turns out the issues we've had with Json.NET are with our linker configuration. If you want to use Json.NET in a Blazor app you can get it to work by using the following linker configuration: https://gist.github.com/Suchiman/a5e65f40c353120c9cf7f6a631ded28b.

Daddoon commented 6 years ago

Thanks a lot @danroth27 (and @Suchiman for the Gist) ! I will take a look on it Monday.

moataz1991 commented 6 years ago

@danroth27 i have same issue , i just don`t know how to use that "linker" !? i don't have any linker in my Blazor project 0.4.0 please help

danroth27 commented 6 years ago

@moataz1991 The linker is run automatically as part of the build of a Blazor project. You can configure what the linker does to your app and get Json.NET to work by updating your .csproj file as shown in https://gist.github.com/Suchiman/a5e65f40c353120c9cf7f6a631ded28b.

moataz1991 commented 6 years ago

@danroth27 ok now i got it , but to which .csproj do i modify and linker.xml to that project ? Server,Client or Shared ?

Suchiman commented 6 years ago

@moataz1991 Client, you want to modify the blazor app itself, not the app that just serves the static files.