Skip to content

Commit

Permalink
Use strongly typed deserializer #211
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewSteeples committed Mar 11, 2024
1 parent 1b1d9eb commit bb9d903
Showing 1 changed file with 10 additions and 10 deletions.
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

0 comments on commit bb9d903

Please sign in to comment.