Skip to content
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

Customize serialization for IJSRuntime.InvokeAsync #11954

Closed
Joelius300 opened this issue Jul 8, 2019 · 3 comments
Closed

Customize serialization for IJSRuntime.InvokeAsync #11954

Joelius300 opened this issue Jul 8, 2019 · 3 comments
Assignees
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

Comments

@Joelius300
Copy link

Issue

Because IJSRuntime.InvokeAsync now uses System.Text.Json instead of Newtonsoft.Json (preview6+) the custom serialization I implemented with Newtonsoft.Json doesn't work anymore.
Even though it says in the issue I linked..

Users may use a JSON serializer of their choice.

.. the IJSRuntime doesn't use Newtonsoft.Json if you add Newtonsoft.Json in the ConfigureServices method using AddNewtonsoftJson on the IMvcBuilder.

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 use Newtonsoft.Json for IJSRuntime.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 for IJSRuntime and cannot pass them in when I call InvokeAsync either, I only know of the following two options:

  • Globally add the options in the startup class. I think this wouldn't be a great idea since it could have unintended sideeffects especially when ignoring null values when serializing.
    By the way, I tried that and it didn't work. See my SO question.
  • Adding an attribute which signals that nulls should be ignored to each class, which in my case would be a lot of classes and it wouldn't look very good + you could easily forget it if you added a new one.

Additional context

  • I'm using the newest preview of .net core 3 (preview6) and it's the first preview this issue occured.
  • I'm working on a blazor library from which I invoke the javascript. The source is on github.
    In this file I inject a IJSRuntime and call extension methods on it which are defined in this file. As you can see in the StripNulls method, I'm currently using ExpandoObject and manual serialization with JsonConvert.SerializeObject and JsonConvert.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 ExpandoObjects in my code. The first step of doing so (which is presumably the easiest way to go) would require the IJSRuntime to use Newtonsoft.Json.
    I already have a branch RemoveExpando where I tried to make IJSRuntime use Newtonsoft.Json (it also has some ugly debug code, don't get scared). Note that the call to IMvcBuilder.AddNewtonsoftJson is in the assembly WebCore and not in ChartJs.Blazor where the injection is. I don't think that should matter but it might be worth mentioning.
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 8, 2019
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @Joelius300.
We don't plan to add extensibility to the IJSRuntime serialization, so that a custom serializer can be plugged in. If you feel there is a feature gap in the existing JSON serializer, feel free to file a feature ask in the dotnet/corefx repo.

@mkArtakMSFT mkArtakMSFT added ✔️ Resolution: Won't Fix Resolved because we decided not to change the behavior reported in this issue. enhancement This issue represents an ask for new feature or an enhancement to an existing one labels Jul 8, 2019
@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Jul 8, 2019
@salaros
Copy link

salaros commented Sep 6, 2019

I hope this bug is going to be fixed BEFORE the final release. It's really critical

@danroth27
Copy link
Member

@salaros As mentioned by @mkArtakMSFT above this isn't something we plan to do because of the impact it can have on any JS interop being used by the app and its dependencies. You need to use the JSON serialization extensibility mechanisms available through System.Text.Json.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 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 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.
Projects
None yet
Development

No branches or pull requests

4 participants