From 015541ad7e72e9ed1305f02e4c3fabf70d0b2a83 Mon Sep 17 00:00:00 2001 From: Todd Cotten Date: Sun, 21 Mar 2021 20:42:54 -0400 Subject: [PATCH] Changed over from System.Text.Json to Newtonsoft.Json --- .../Blazored.SessionStorage.csproj | 1 + .../ServiceCollectionExtensions.cs | 50 +++++++++---------- .../SessionStorageService.cs | 30 ++++++----- .../StorageOptions/SessionStorageOptions.cs | 14 ++---- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/Blazored.SessionStorage/Blazored.SessionStorage.csproj b/src/Blazored.SessionStorage/Blazored.SessionStorage.csproj index 3226b58..a1f72fc 100644 --- a/src/Blazored.SessionStorage/Blazored.SessionStorage.csproj +++ b/src/Blazored.SessionStorage/Blazored.SessionStorage.csproj @@ -29,6 +29,7 @@ + diff --git a/src/Blazored.SessionStorage/ServiceCollectionExtensions.cs b/src/Blazored.SessionStorage/ServiceCollectionExtensions.cs index 4bb05e7..c2c7039 100644 --- a/src/Blazored.SessionStorage/ServiceCollectionExtensions.cs +++ b/src/Blazored.SessionStorage/ServiceCollectionExtensions.cs @@ -1,33 +1,33 @@ using System; using Blazored.SessionStorage.JsonConverters; using Blazored.SessionStorage.StorageOptions; -using Microsoft.Extensions.DependencyInjection; - -namespace Blazored.SessionStorage -{ - public static class ServiceCollectionExtensions - { - public static IServiceCollection AddBlazoredSessionStorage(this IServiceCollection services) - { - return services - .AddScoped() - .AddScoped() - .Configure(configureOptions => - { - configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); - }); - } +using Microsoft.Extensions.DependencyInjection; + +namespace Blazored.SessionStorage +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddBlazoredSessionStorage(this IServiceCollection services) + { + return services + .AddScoped() + .AddScoped(); + //.Configure(configureOptions => + //{ + // configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); + //}); + } public static IServiceCollection AddBlazoredSessionStorage(this IServiceCollection services, Action configure) { return services - .AddScoped() - .AddScoped() - .Configure(configureOptions => - { - configure?.Invoke(configureOptions); - configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); - }); + .AddScoped() + .AddScoped(); + //.Configure(configureOptions => + //{ + // configure?.Invoke(configureOptions); + // configureOptions.JsonSerializerOptions.Converters.Add(new TimespanJsonConverter()); + //}); } - } -} + } +} diff --git a/src/Blazored.SessionStorage/SessionStorageService.cs b/src/Blazored.SessionStorage/SessionStorageService.cs index fbbe878..8a93021 100644 --- a/src/Blazored.SessionStorage/SessionStorageService.cs +++ b/src/Blazored.SessionStorage/SessionStorageService.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Options; using Microsoft.JSInterop; using System; -using System.Text.Json; +using Newtonsoft.Json; using System.Threading.Tasks; namespace Blazored.SessionStorage @@ -11,7 +11,7 @@ public class SessionStorageService : ISessionStorageService, ISyncSessionStorage { private readonly IJSRuntime _jSRuntime; private readonly IJSInProcessRuntime _jSInProcessRuntime; - private readonly JsonSerializerOptions _jsonOptions; + private readonly JsonSerializerSettings _jsonOptions; public event EventHandler Changing; @@ -20,7 +20,7 @@ public class SessionStorageService : ISessionStorageService, ISyncSessionStorage public SessionStorageService(IJSRuntime jSRuntime, IOptions options) { _jSRuntime = jSRuntime; - _jsonOptions = options.Value.JsonSerializerOptions; + _jsonOptions = options.Value.JsonSerializerSettings; _jSInProcessRuntime = jSRuntime as IJSInProcessRuntime; } @@ -34,9 +34,10 @@ public async Task SetItemAsync(string key, T data) if (e.Cancel) return; - var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); + var serializedData = JsonConvert.SerializeObject(data, _jsonOptions); + //var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); - await _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, serialisedData); + await _jSRuntime.InvokeVoidAsync("sessionStorage.setItem", key, serializedData); RaiseOnChanged(key, e.OldValue, data); } @@ -46,12 +47,13 @@ public async Task GetItemAsync(string key) if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key)); - var serialisedData = await _jSRuntime.InvokeAsync("sessionStorage.getItem", key); + var serializedData = await _jSRuntime.InvokeAsync("sessionStorage.getItem", key); - if (serialisedData == null) + if (serializedData == null) return default; - return JsonSerializer.Deserialize(serialisedData, _jsonOptions); + return JsonConvert.DeserializeObject(serializedData, _jsonOptions); + //return JsonSerializer.Deserialize(serialisedData, _jsonOptions); } public async Task RemoveItemAsync(string key) @@ -83,9 +85,10 @@ public void SetItem(string key, T data) if (e.Cancel) return; - var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); + var serializedData = JsonConvert.SerializeObject(data, _jsonOptions); + //var serialisedData = JsonSerializer.Serialize(data, _jsonOptions); - _jSInProcessRuntime.InvokeVoid("sessionStorage.setItem", key, serialisedData); + _jSInProcessRuntime.InvokeVoid("sessionStorage.setItem", key, serializedData); RaiseOnChanged(key, e.OldValue, data); } @@ -98,12 +101,13 @@ public T GetItem(string key) if (_jSInProcessRuntime == null) throw new InvalidOperationException("IJSInProcessRuntime not available"); - var serialisedData = _jSInProcessRuntime.Invoke("sessionStorage.getItem", key); + var serializedData = _jSInProcessRuntime.Invoke("sessionStorage.getItem", key); - if (serialisedData == null) + if (serializedData == null) return default; - return JsonSerializer.Deserialize(serialisedData, _jsonOptions); + return JsonConvert.DeserializeObject(serializedData, _jsonOptions); + //return JsonSerializer.Deserialize(serialisedData, _jsonOptions); } public void RemoveItem(string key) diff --git a/src/Blazored.SessionStorage/StorageOptions/SessionStorageOptions.cs b/src/Blazored.SessionStorage/StorageOptions/SessionStorageOptions.cs index b005f1a..f95ce42 100644 --- a/src/Blazored.SessionStorage/StorageOptions/SessionStorageOptions.cs +++ b/src/Blazored.SessionStorage/StorageOptions/SessionStorageOptions.cs @@ -1,18 +1,10 @@ -using System.Text.Json; +using Newtonsoft.Json; +using System.Text.Json; namespace Blazored.SessionStorage.StorageOptions { public class SessionStorageOptions { - public JsonSerializerOptions JsonSerializerOptions { get; } = new JsonSerializerOptions - { - DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, - IgnoreNullValues = true, - IgnoreReadOnlyProperties = true, - PropertyNameCaseInsensitive = true, - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - ReadCommentHandling = JsonCommentHandling.Skip, - WriteIndented = false - }; + public JsonSerializerSettings JsonSerializerSettings { get; set; } } }