Skip to content

Trying to convert JSON to Object using Newtonsoft freezes #16001

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

Closed
tessierp opened this issue May 17, 2018 · 8 comments
Closed

Trying to convert JSON to Object using Newtonsoft freezes #16001

tessierp opened this issue May 17, 2018 · 8 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@tessierp
Copy link

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
Copy link
Member

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
Copy link

Daddoon commented Jun 22, 2018

@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
Copy link
Member

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
Copy link

Daddoon commented Jun 22, 2018

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

@moataz1991
Copy link

@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
Copy link
Member

@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
Copy link

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

@Suchiman
Copy link
Contributor

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

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

6 participants