Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforcing Blazor webassembly to use Newtonsoft.Json on client side

In Blazor webassembly is there a way to enforce the use of Newtonsoft.Json serlialization for httpclient and methods such as GetJsonAsync, PostAsJsonAsync etc.

Or the only way is to write helper methods and use GetAsync and then serialize the response manually using Newtonsoft.Json?

like image 311
mko Avatar asked Jun 01 '20 08:06

mko


People also ask

Is Blazor WebAssembly client-side?

Blazor WebAssembly (WASM) apps run client-side in the browser on a WebAssembly-based . NET runtime. The Blazor app, its dependencies, and the . NET runtime are downloaded to the browser.

How do I call a web API from Blazor WebAssembly?

In the Start folder, you can find two projects. The client application from the previous article, and the server Web API application. Since the Web API works with data from the SQL database, all you have to do is to modify the connection string in the appsettings. json file and start the application.


1 Answers

is there a way to enforce the use of Newtonsoft.Json for ... PostAsJsonAsync etc.

No. This has been shifting around a bit lately, especially for Blazor. But the System.Net.Http.Json.HttpClientJsonExtensions are now directly dependent on System.Text.Json, not pluggable.

So yes, you will have to write your own helpers, not too big a deal. The linker might even remove the System.Text.Json classes in a Release build, I'm not sure.

But the counter question is of course why you need this? NewtonSoft is legacy from now on and if you really have incompatible data maybe ask if you can resolve this with some System.Text options.

like image 165
Henk Holterman Avatar answered Oct 07 '22 13:10

Henk Holterman