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

Use strongly typed deserializer #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/Blazored.LocalStorage/LocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async ValueTask SetItemAsync<T>(string key, T data, CancellationToken can
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

var e = await RaiseOnChangingAsync(key, data).ConfigureAwait(false);
var e = await RaiseOnChangingAsync<T>(key, data).ConfigureAwait(false);

if (e.Cancel)
return;
Expand All @@ -42,7 +42,7 @@ public async ValueTask SetItemAsStringAsync(string key, string data, Cancellatio
if (data is null)
throw new ArgumentNullException(nameof(data));

var e = await RaiseOnChangingAsync(key, data).ConfigureAwait(false);
var e = await RaiseOnChangingAsync<string>(key, data).ConfigureAwait(false);

if (e.Cancel)
return;
Expand Down Expand Up @@ -118,7 +118,7 @@ public void SetItem<T>(string key, T data)
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

var e = RaiseOnChangingSync(key, data);
var e = RaiseOnChangingSync<T>(key, data);

if (e.Cancel)
return;
Expand All @@ -136,7 +136,7 @@ public void SetItemAsString(string key, string data)

ArgumentNullException.ThrowIfNull(data);

var e = RaiseOnChangingSync(key, data);
var e = RaiseOnChangingSync<string>(key, data);

if (e.Cancel)
return;
Expand Down Expand Up @@ -208,12 +208,12 @@ public bool ContainKey(string key)
=> _storageProvider.ContainKey(key);

public event EventHandler<ChangingEventArgs>? Changing;
private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object? data)
private async Task<ChangingEventArgs> RaiseOnChangingAsync<T>(string key, object? data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = await GetItemInternalAsync<object>(key).ConfigureAwait(false),
OldValue = await GetItemInternalAsync<T>(key).ConfigureAwait(false),
NewValue = data
};

Expand All @@ -222,12 +222,12 @@ private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object? d
return e;
}

private ChangingEventArgs RaiseOnChangingSync(string key, object? data)
private ChangingEventArgs RaiseOnChangingSync<T>(string key, object? data)
{
var e = new ChangingEventArgs
{
Key = key,
OldValue = GetItemInternal(key),
OldValue = GetItemInternal<T>(key),
NewValue = data
};

Expand Down Expand Up @@ -255,7 +255,7 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object? data)
}
}

private object? GetItemInternal(string key)
private object? GetItemInternal<T>(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -267,7 +267,7 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object? data)

try
{
return _serializer.Deserialize<object>(serialisedData);
return _serializer.Deserialize<T>(serialisedData);
}
catch (JsonException)
{
Expand Down