diff --git a/src/JSInterop/Microsoft.JSInterop/src/DotNetDispatcher.cs b/src/JSInterop/Microsoft.JSInterop/src/DotNetDispatcher.cs index d7fc99f484..360efae5d1 100644 --- a/src/JSInterop/Microsoft.JSInterop/src/DotNetDispatcher.cs +++ b/src/JSInterop/Microsoft.JSInterop/src/DotNetDispatcher.cs @@ -51,7 +51,7 @@ namespace Microsoft.JSInterop return null; } - return JsonSerializer.ToString(syncResult, JsonSerializerOptionsProvider.Options); + return JsonSerializer.Serialize(syncResult, JsonSerializerOptionsProvider.Options); } /// @@ -177,9 +177,9 @@ namespace Microsoft.JSInterop var shouldDisposeJsonDocument = true; try { - if (jsonDocument.RootElement.Type != JsonValueType.Array) + if (jsonDocument.RootElement.ValueKind != JsonValueKind.Array) { - throw new ArgumentException($"Expected a JSON array but got {jsonDocument.RootElement.Type}."); + throw new ArgumentException($"Expected a JSON array but got {jsonDocument.RootElement.ValueKind}."); } var suppliedArgsLength = jsonDocument.RootElement.GetArrayLength(); @@ -211,7 +211,7 @@ namespace Microsoft.JSInterop } else { - suppliedArgs[index] = JsonSerializer.Parse(item.GetRawText(), parameterType, JsonSerializerOptionsProvider.Options); + suppliedArgs[index] = JsonSerializer.Deserialize(item.GetRawText(), parameterType, JsonSerializerOptionsProvider.Options); } index++; @@ -236,7 +236,7 @@ namespace Microsoft.JSInterop // Check for incorrect use of DotNetObjectRef at the top level. We know it's // an incorrect use if there's a object that looks like { '__dotNetObject': }, // but we aren't assigning to DotNetObjectRef{T}. - return item.Type == JsonValueType.Object && + return item.ValueKind == JsonValueKind.Object && item.TryGetProperty(DotNetObjectRefKey, out _) && !typeof(IDotNetObjectRef).IsAssignableFrom(parameterType); } diff --git a/src/JSInterop/Microsoft.JSInterop/src/JSInProcessRuntimeBase.cs b/src/JSInterop/Microsoft.JSInterop/src/JSInProcessRuntimeBase.cs index 2565496a54..7606f1865f 100644 --- a/src/JSInterop/Microsoft.JSInterop/src/JSInProcessRuntimeBase.cs +++ b/src/JSInterop/Microsoft.JSInterop/src/JSInProcessRuntimeBase.cs @@ -19,13 +19,13 @@ namespace Microsoft.JSInterop /// An instance of obtained by JSON-deserializing the return value. public TValue Invoke(string identifier, params object[] args) { - var resultJson = InvokeJS(identifier, JsonSerializer.ToString(args, JsonSerializerOptionsProvider.Options)); + var resultJson = InvokeJS(identifier, JsonSerializer.Serialize(args, JsonSerializerOptionsProvider.Options)); if (resultJson is null) { return default; } - return JsonSerializer.Parse(resultJson, JsonSerializerOptionsProvider.Options); + return JsonSerializer.Deserialize(resultJson, JsonSerializerOptionsProvider.Options); } /// diff --git a/src/JSInterop/Microsoft.JSInterop/src/JSRuntimeBase.cs b/src/JSInterop/Microsoft.JSInterop/src/JSRuntimeBase.cs index e2b3875a50..50fd7e2839 100644 --- a/src/JSInterop/Microsoft.JSInterop/src/JSRuntimeBase.cs +++ b/src/JSInterop/Microsoft.JSInterop/src/JSRuntimeBase.cs @@ -42,7 +42,7 @@ namespace Microsoft.JSInterop try { var argsJson = args?.Length > 0 ? - JsonSerializer.ToString(args, JsonSerializerOptionsProvider.Options) : + JsonSerializer.Serialize(args, JsonSerializerOptionsProvider.Options) : null; BeginInvokeJS(taskId, identifier, argsJson); return tcs.Task; @@ -78,7 +78,7 @@ namespace Microsoft.JSInterop // We pass 0 as the async handle because we don't want the JS-side code to // send back any notification (we're just providing a result for an existing async call) - var args = JsonSerializer.ToString(new[] { callId, success, resultOrException }, JsonSerializerOptionsProvider.Options); + var args = JsonSerializer.Serialize(new[] { callId, success, resultOrException }, JsonSerializerOptionsProvider.Options); BeginInvokeJS(0, "DotNet.jsCallDispatcher.endInvokeDotNetFromJS", args); } @@ -97,7 +97,7 @@ namespace Microsoft.JSInterop try { var result = asyncCallResult != null ? - JsonSerializer.Parse(asyncCallResult.JsonElement.GetRawText(), resultType, JsonSerializerOptionsProvider.Options) : + JsonSerializer.Deserialize(asyncCallResult.JsonElement.GetRawText(), resultType, JsonSerializerOptionsProvider.Options) : null; TaskGenericsUtil.SetTaskCompletionSourceResult(tcs, result); } diff --git a/src/JSInterop/Microsoft.JSInterop/test/DotNetDispatcherTest.cs b/src/JSInterop/Microsoft.JSInterop/test/DotNetDispatcherTest.cs index 7854122feb..4e8733e067 100644 --- a/src/JSInterop/Microsoft.JSInterop/test/DotNetDispatcherTest.cs +++ b/src/JSInterop/Microsoft.JSInterop/test/DotNetDispatcherTest.cs @@ -89,7 +89,7 @@ namespace Microsoft.JSInterop.Tests { // Arrange/Act var resultJson = DotNetDispatcher.Invoke(thisAssemblyName, "InvocableStaticNonVoid", default, null); - var result = JsonSerializer.Parse(resultJson, JsonSerializerOptionsProvider.Options); + var result = JsonSerializer.Deserialize(resultJson, JsonSerializerOptionsProvider.Options); // Assert Assert.Equal("Test", result.StringVal); @@ -101,7 +101,7 @@ namespace Microsoft.JSInterop.Tests { // Arrange/Act var resultJson = DotNetDispatcher.Invoke(thisAssemblyName, nameof(SomePublicType.InvokableMethodWithoutCustomIdentifier), default, null); - var result = JsonSerializer.Parse(resultJson, JsonSerializerOptionsProvider.Options); + var result = JsonSerializer.Deserialize(resultJson, JsonSerializerOptionsProvider.Options); // Assert Assert.Equal("InvokableMethodWithoutCustomIdentifier", result.StringVal); @@ -117,7 +117,7 @@ namespace Microsoft.JSInterop.Tests jsRuntime.Invoke("unimportant", objectRef); // Arrange: Remaining args - var argsJson = JsonSerializer.ToString(new object[] + var argsJson = JsonSerializer.Serialize(new object[] { new TestDTO { StringVal = "Another string", IntVal = 456 }, new[] { 100, 200 }, @@ -130,7 +130,7 @@ namespace Microsoft.JSInterop.Tests var root = result.RootElement; // Assert: First result value marshalled via JSON - var resultDto1 = JsonSerializer.Parse(root[0].GetRawText(), JsonSerializerOptionsProvider.Options); + var resultDto1 = JsonSerializer.Deserialize(root[0].GetRawText(), JsonSerializerOptionsProvider.Options); Assert.Equal("ANOTHER STRING", resultDto1.StringVal); Assert.Equal(756, resultDto1.IntVal); @@ -156,7 +156,7 @@ namespace Microsoft.JSInterop.Tests jsRuntime.Invoke("unimportant", objectRef); // Arrange: Remaining args - var argsJson = JsonSerializer.ToString(new object[] + var argsJson = JsonSerializer.Serialize(new object[] { new TestDTO { StringVal = "Another string", IntVal = 456 }, new[] { 100, 200 }, @@ -262,7 +262,7 @@ namespace Microsoft.JSInterop.Tests public void CannotInvokeWithIncorrectNumberOfParams() { // Arrange - var argsJson = JsonSerializer.ToString(new object[] { 1, 2, 3, 4 }, JsonSerializerOptionsProvider.Options); + var argsJson = JsonSerializer.Serialize(new object[] { 1, 2, 3, 4 }, JsonSerializerOptionsProvider.Options); // Act/Assert var ex = Assert.Throws(() => @@ -284,7 +284,7 @@ namespace Microsoft.JSInterop.Tests jsRuntime.Invoke("unimportant", arg1Ref, arg2Ref); // Arrange: all args - var argsJson = JsonSerializer.ToString(new object[] + var argsJson = JsonSerializer.Serialize(new object[] { new TestDTO { IntVal = 1000, StringVal = "String via JSON" }, arg2Ref, @@ -306,12 +306,12 @@ namespace Microsoft.JSInterop.Tests Assert.True(result[1].GetBoolean()); // Success flag // Assert: First result value marshalled via JSON - var resultDto1 = JsonSerializer.Parse(resultValue[0].GetRawText(), JsonSerializerOptionsProvider.Options); + var resultDto1 = JsonSerializer.Deserialize(resultValue[0].GetRawText(), JsonSerializerOptionsProvider.Options); Assert.Equal("STRING VIA JSON", resultDto1.StringVal); Assert.Equal(2000, resultDto1.IntVal); // Assert: Second result value marshalled by ref - var resultDto2Ref = JsonSerializer.Parse>(resultValue[1].GetRawText(), JsonSerializerOptionsProvider.Options); + var resultDto2Ref = JsonSerializer.Deserialize>(resultValue[1].GetRawText(), JsonSerializerOptionsProvider.Options); var resultDto2 = resultDto2Ref.Value; Assert.Equal("MY STRING", resultDto2.StringVal); Assert.Equal(2468, resultDto2.IntVal); diff --git a/src/JSInterop/Microsoft.JSInterop/test/JSRuntimeBaseTest.cs b/src/JSInterop/Microsoft.JSInterop/test/JSRuntimeBaseTest.cs index 4e0818219b..afbe5d0595 100644 --- a/src/JSInterop/Microsoft.JSInterop/test/JSRuntimeBaseTest.cs +++ b/src/JSInterop/Microsoft.JSInterop/test/JSRuntimeBaseTest.cs @@ -111,7 +111,7 @@ namespace Microsoft.JSInterop.Tests Assert.IsType(jsException.InnerException); // Verify we've disposed the JsonDocument. - Assert.Throws(() => jsonDocument.RootElement.Type); + Assert.Throws(() => jsonDocument.RootElement.ValueKind); } [Fact]