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

[Discussion]: Breaking changes to Microsoft.JSInterop #10810

Closed
pranavkm opened this issue Jun 3, 2019 · 1 comment
Closed

[Discussion]: Breaking changes to Microsoft.JSInterop #10810

pranavkm opened this issue Jun 3, 2019 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components breaking-change This issue / pr will introduce a breaking change, when resolved / merged.
Milestone

Comments

@pranavkm
Copy link
Contributor

pranavkm commented Jun 3, 2019

In 3.0-preview6, we're migrating Microsoft.JSInterop to use the System.Text.Json based serializer. As part of this transition, there are several breaking changes to the JSInterop library:

  • The helper type Microsoft.JSInterop.Json is being removed. Users may use a JSON serializer of their choice. We recommend using the System.Text.Json-based serializer since Blazor already references it. Here's the code change required to migrate to use System.Text.Json:
// Before
string value = Json.Serialize(...);
MyPoco poco = Json.Deserialize<MyPoco>("...");

// After
using System.Text.Json.Serialization;
...

string value = JsonSerializer.ToString(...);
MyPoco poco = JsonSerializer.Parse<MyPoco>("...");
  • DotNetObjectRef is replaced by a generic DotNetObjectRef<T> that is required for both sending and receiving a managed object reference when doing interop with the browser
// Before
IJSRuntime.InvokeAsync("SomeJSMethod", new DotNetObjectRef(someObject));

[JSInvokable]
public static Task SomeDotNetMethod(MyObject myObject)
{
    ...
}

// After
IJSRuntime.InvokeAsync("SomeMethod", DotNetObjectRef.Create(someObject));

[JSInvokable]
public static Task SomeDotNetMethod(DotNetObjectRef<MyObject> myObject)
{
    ...
}
  • Microsoft.JSInterop will use System.Text.Json to marshal interop data. To customize serialization, you may use serialization primitives such as JsonPropertyNameAttribute, JsonIgnoreAttribute etc.

  • IJSRuntime.UntrackObjectRef(DotNetObjectRef); has been removed. To stop tracking an object reference, dispose the DotNetObjectRef<T> instance either on the server or the client.

@pranavkm pranavkm added this to the Discussions milestone Jun 3, 2019
@mkArtakMSFT mkArtakMSFT added the breaking-change This issue / pr will introduce a breaking change, when resolved / merged. label Jun 3, 2019
@pranavkm pranavkm added the area-blazor Includes: Blazor, Razor Components label Jun 3, 2019
@Joelius300
Copy link

I switched from the System.Text.Json-based serializer back to json.net following these ms-docs. I'm using a server-side blazor project (preview6) and called AddNewtonsoftJson on the IMvcBuilder I got back from services.AddRazorPages() in the ConfigureServices method.

The problem is that it still uses the System.Text.Json-based serializer when invoking javascript using IJSRuntime.InvokeAsync. I have already opened a SO question for this issue.

Is the way I change the serializer not correct?
What do I have to do for IJSRuntime to use json.net?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components breaking-change This issue / pr will introduce a breaking change, when resolved / merged.
Projects
None yet
Development

No branches or pull requests

3 participants