-
Notifications
You must be signed in to change notification settings - Fork 10k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Blazor] Use JSON source generator during WebAssembly startup (#54956)
- Loading branch information
1 parent
23afddf
commit c9af79a
Showing
19 changed files
with
238 additions
and
90 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/Components/Shared/src/JsonSerialization/JsonConverterFactoryTypeInfoResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
|
||
namespace Microsoft.AspNetCore.Components; | ||
|
||
// For custom converters that don't rely on serializing an object graph, | ||
// we can resolve the incoming type's JsonTypeInfo directly from the converter. | ||
// This skips extra work to collect metadata for the type that won't be used. | ||
internal sealed class JsonConverterFactoryTypeInfoResolver<T> : IJsonTypeInfoResolver | ||
{ | ||
public static readonly JsonConverterFactoryTypeInfoResolver<T> Instance = new(); | ||
|
||
private JsonConverterFactoryTypeInfoResolver() | ||
{ | ||
} | ||
|
||
public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options) | ||
{ | ||
if (type != typeof(T)) | ||
{ | ||
return null; | ||
} | ||
|
||
foreach (var converter in options.Converters) | ||
{ | ||
if (converter is not JsonConverterFactory factory || !factory.CanConvert(type)) | ||
{ | ||
continue; | ||
} | ||
|
||
if (factory.CreateConverter(type, options) is not { } converterToUse) | ||
{ | ||
continue; | ||
} | ||
|
||
return JsonMetadataServices.CreateValueInfo<T>(options, converterToUse); | ||
} | ||
|
||
return null; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Components/Web/src/Internal/IInternalWebJSInProcessRuntime.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Internal; | ||
|
||
/// <summary> | ||
/// For internal framework use only. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public interface IInternalWebJSInProcessRuntime | ||
{ | ||
/// <summary> | ||
/// For internal framework use only. | ||
/// </summary> | ||
string InvokeJS(string identifier, [StringSyntax(StringSyntaxAttribute.Json)] string? argsJson, JSCallResultType resultType, long targetInstanceId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Components.Web.Internal.IInternalWebJSInProcessRuntime | ||
Microsoft.AspNetCore.Components.Web.Internal.IInternalWebJSInProcessRuntime.InvokeJS(string! identifier, string? argsJson, Microsoft.JSInterop.JSCallResultType resultType, long targetInstanceId) -> string! | ||
Microsoft.AspNetCore.Components.Web.KeyboardEventArgs.IsComposing.get -> bool | ||
Microsoft.AspNetCore.Components.Web.KeyboardEventArgs.IsComposing.set -> void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.