8

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?

mko
  • 4,825
  • 8
  • 45
  • 97

1 Answers1

5

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.

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
  • 3
    Well the use of attributes such as [JsonProperty("model")] [JsonConverter(typeof(StringEnumConverter))] was being very usefull to me. I didnt know there was an alternative, but after your answer you made me think. I have found the following link https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-apis/ It looks like there is a proper alternative – mko Jun 01 '20 at 10:09
  • 1
    The problem is System.Text.Json is still largely a burning trash fire. It doesn't support language specific things like TypeConverters. That should have been baked in long before they even got started let alone rolled out a released product. – Buvy Aug 12 '20 at 22:44
  • 1
    I now stepped into the problem, that System.Text.Json does not support the `[JsonConstructor]` attribute. So now I have to equip all my DTOs with public setters. Yeah sure, I can do that, but that would break the expected immutability of DTOs. And I will certainly not write my own parser as officially suggested here (https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#specify-constructor-to-use). – PuerNoctis Sep 27 '20 at 09:57
  • 1
    Also unless I'm mistaken System.Text.Json.JsonIgnoreAttribute isn't available for .Net Standard 2.1. The only JsonIgnoreAttribute that I can seem to reference in my Blazor app is the Newtonsoft one, and System.Text.Json / System.Net.Http.Json ignores that attribute so I currently have no way to exclude a property from serialisation. I'm working around it by making the property protected and rewriting calling code to go via an intermediary which is obviously a whole lot of fun. – Jon Jan 13 '21 at 11:17
  • NewtonSoft does still have some advantages - In my case I wanted to deserialize to `Dictionary` in order to create an `ExpandoObject`. `System.Text.Json` deserialized to `JsonElement` whereas Json.Net deserialized to anonymous, which is what I wanted. – Luke T O'Brien Apr 19 '21 at 15:03