Customize serialization for IJSRuntime.InvokeAsync #11954
Labels
area-blazor
Includes: Blazor, Razor Components
enhancement
This issue represents an ask for new feature or an enhancement to an existing one
✔️ Resolution: Won't Fix
Resolved because we decided not to change the behavior reported in this issue.
Milestone
Issue
Because
IJSRuntime.InvokeAsync
now usesSystem.Text.Json
instead ofNewtonsoft.Json
(preview6+) the custom serialization I implemented withNewtonsoft.Json
doesn't work anymore.Even though it says in the issue I linked..
.. the
IJSRuntime
doesn't useNewtonsoft.Json
if you addNewtonsoft.Json
in theConfigureServices
method usingAddNewtonsoftJson
on theIMvcBuilder
.I have already made a comment in the discussion for this issue describing the issue and what I already tried (back then I didn't know this wasn't the default behaviour). In this comment I also link to my SO question where you can find further information on the attempts I've made (same as before, I assumed this wasn't default behaviour).
Request / Idea
I would really like to be able to change what Serializer is used for
IJSRuntime.InvokeAsync
.In my opinion the most obvious behaviour would be to use the one that's used everywhere else. This would mean that if you specify to use
Newtonsoft.Json
for the internal serialization, it would also useNewtonsoft.Json
forIJSRuntime.InvokeAsync
.It would also be great if you could tell the
IJSRuntime
to use specific options for the serialization.In my case I want the
IJSRuntime
to ignore all null values but since I cannot specify any options forIJSRuntime
and cannot pass them in when I callInvokeAsync
either, I only know of the following two options:By the way, I tried that and it didn't work. See my SO question.
Additional context
In this file I inject a
IJSRuntime
and call extension methods on it which are defined in this file. As you can see in theStripNulls
method, I'm currently usingExpandoObject
and manual serialization withJsonConvert.SerializeObject
andJsonConvert.DeserializeObject<ExpandoObject>
as a workaround to remove all null values and make use the custom converters (i.e. this one).This works but it's very hacky and I would like to get rid of all
ExpandoObject
s in my code. The first step of doing so (which is presumably the easiest way to go) would require theIJSRuntime
to useNewtonsoft.Json
.I already have a branch RemoveExpando where I tried to make
IJSRuntime
useNewtonsoft.Json
(it also has some ugly debug code, don't get scared). Note that the call toIMvcBuilder.AddNewtonsoftJson
is in the assemblyWebCore
and not inChartJs.Blazor
where the injection is. I don't think that should matter but it might be worth mentioning.The text was updated successfully, but these errors were encountered: