-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Labels
area-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Components
Milestone
Description
Currently we only serialize things as json. Ideally, we need an interface or something that you can implement (or pass as a parameter in the attribute) to provide a custom serialization mechanism.
The interface will be something like
public interface IPersistentComponentStateSerializer
{
Task Persist(Type type, object instance, IBufferWriter buffer);
void Restore(Type type, ReadOnlySequence<byte> data);
}
- Restore needs to be synchronous to avoid tearing on the UI.
- We don't want
byte[]
based APIs, as we want to minimize additional allocations if possible and we don't want to give the serializer the responsibility of allocating buffers.
Implementation Plan for Serialization Extensibility in Persistent Component State
Sample Registration
// Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
// Register custom serializer as Singleton
builder.Services.AddSingleton<IPersistentComponentStateSerializer<User>, CustomUserSerializer>();
var app = builder.Build();
Task Checklist
1. Define the Serializer Interface
- Create IPersistentComponentStateSerializer interfaceDefine PersistAsync(T value, IBufferWriter writer, CancellationToken cancellationToken) methodDefine Restore(ReadOnlySequence data) method (synchronous)Add appropriate XML documentation for the interface and methodsTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
2. Update PooledArrayBufferWriter Integration
- Ensure PooledArrayBufferWriter from /src/Shared/PooledArrayBufferWriter.cs is accessibleVerify compatibility with IBufferWriter interface for the Persist methodAdd any necessary using statements or referencesTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
3. Modify Persistent State Value Provider
- Update the persistent state value provider to attempt DI resolution of IPersistentComponentStateSerializerImplement fallback logic to use default JSON serialization when no custom serializer is foundHandle type-specific serializer resolutionEnsure proper error handling for serializer resolution failuresTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
5. Maintain Backward Compatibility
- Ensure existing JSON serialization continues to work unchangedVerify no breaking changes to existing persistent component state APIsTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
6. Add Tests
- Unit tests for the new interface implementationTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Copilot
Metadata
Metadata
Labels
area-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Components
Activity